<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://nhforge.org/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>How to</title><link>http://nhforge.org/wikis/howtonh/default.aspx</link><description>Quick starts, tutorials, code snippets, custom user types, application blocks and more...</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/0.aspx</link><pubDate>Sat, 28 Jan 2012 22:29:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:97</guid><dc:creator>awgneo</dc:creator><description>Current revision posted to How to by awgneo on 28/01/2012 07:29:15 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;loading&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FluentNH&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;I found many examples online covering how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);&lt;/span&gt;
 
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;resolvedEntities)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);&lt;/span&gt;
 
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;resolvedEntities)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(entity,&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;List&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Object&amp;gt;());&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;List&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
&lt;p&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;Example&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computerQuery&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;c&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;context.Session.Query&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Types.Computer&amp;gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;where&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;c.ID&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computerID&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;select&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;c&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computer&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Resolver.Resolve&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Types.Computer&amp;gt;(computerQuery.SingleOrDefault(),&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Thanks,&lt;br /&gt;Alex G. &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;awgneo@xbetanet.com)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/4.aspx</link><pubDate>Sat, 28 Jan 2012 22:23:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:532</guid><dc:creator>awgneo</dc:creator><description>Revision 4 posted to How to by awgneo on 28/01/2012 07:23:22 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading, Nhibernate, FluentNH&lt;/div&gt;

&lt;p&gt;I found many examples online covering how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/3.aspx</link><pubDate>Sat, 28 Jan 2012 22:22:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:531</guid><dc:creator>awgneo</dc:creator><description>Revision 3 posted to How to by awgneo on 28/01/2012 07:22:37 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading, Nhibernate, FluentNH&lt;/div&gt;

&lt;p&gt;I found many examples online &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;covering&lt;/span&gt; how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/2.aspx</link><pubDate>Sat, 28 Jan 2012 22:22:19 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:530</guid><dc:creator>awgneo</dc:creator><description>Revision 2 posted to How to by awgneo on 28/01/2012 07:22:19 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading, Nhibernate, FluentNH&lt;/div&gt;

&lt;p&gt;I found many &lt;span style="text-decoration: line-through; color: red;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;examples&lt;/span&gt; online on how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/1.aspx</link><pubDate>Sat, 28 Jan 2012 22:21:48 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:529</guid><dc:creator>awgneo</dc:creator><description>Revision 1 posted to How to by awgneo on 28/01/2012 07:21:48 p.m.&lt;br /&gt;
&lt;p&gt;I found many example online on how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/b&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Standard Object Graph Walking&lt;/b&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;/p&gt;</description></item><item><title>Mapping tables</title><link>http://nhforge.org/wikis/howtonh/mapping-tables/revision/0.aspx</link><pubDate>Wed, 23 Nov 2011 15:01:29 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:96</guid><dc:creator>oceantrain</dc:creator><description>Current revision posted to How to by oceantrain on 23/11/2011 12:01:29 p.m.&lt;br /&gt;
&lt;p&gt;I&amp;#39;m new to NHibernate, So I&amp;#39;m not sure if I this is even the place to ask questions. But here goes.&lt;/p&gt;
&lt;p&gt; This is my problem:&lt;/p&gt;
&lt;p&gt;I am building a website using Orchard CMS. &amp;nbsp;All the Orchard tables are mapped by NHibernate. I also have tables in the same database that are NOT mapped by NHibernate. These tables were provided by the client. I need to query these tables and display data on the front end using Orchard. I must keep the table schemas in tact. I have a tutorial on how to map tables using NHibernate, however everything I see in the tutorials, requires the tables to have an ID column as the Primary key. None of the tables provided to me by the client have ID as the first/Primary key column. Is it possible to map to these tables without using the ID method? Or a variation of the ID method?&lt;/p&gt;
&lt;p&gt;Id(x =&amp;gt; x.Id);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Instead can I map to the table using:&lt;/p&gt;
&lt;p&gt;MyColumnName/Primarykey(x =&amp;gt; x.Id);&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;Id(x =&amp;gt; x.MyColumnname/PrimaryKey);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Again, at this point I cannot migrate the data into new tables mapped by NHibernate and also the table schema must remain in tact. As the Client is updating these tables on a nightly bases.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Any help is appreciated.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Oceantrain&lt;/p&gt;
&lt;p&gt;:&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/0.aspx</link><pubDate>Sun, 20 Nov 2011 23:29:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:65</guid><dc:creator>Steve Bohlen</dc:creator><description>Current revision posted to How to by Steve Bohlen on 20/11/2011 08:29:17 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IIdentifierGenerator&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IPersistentIdentifierGenerator&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;TableGenerator&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FluentNH&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator.&lt;/code&gt;&amp;nbsp; &amp;nbsp;I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Source&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://packshot360.wordpress.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;packshot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;360&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/10.aspx</link><pubDate>Sun, 20 Nov 2011 22:56:44 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:528</guid><dc:creator>marcusix</dc:creator><description>Revision 10 posted to How to by marcusix on 20/11/2011 07:56:44 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&lt;a href="http://www.essaymojo.co.uk/buy-essay.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;a href="http://www.essaymojo.co.uk/write-my-essay.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Write&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;My&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;For&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Me&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Cur&lt;/span&gt;&lt;a href="http://ideal-escapes.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rent&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ly&lt;/span&gt; &lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Currently&lt;/span&gt; I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Hill&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;About&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Woodfield&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;a href="http://www.essayinn.co.uk"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;UK&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Rochville&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Adisonhighschool&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Corllins&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Review&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;a href="http://www.essaymojo.co.uk/essay-service.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Service&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator.&lt;a href="http://www.mightydesigners.com"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Logo&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Contests&lt;/span&gt;&lt;/code&gt;&amp;nbsp; &amp;nbsp;I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Source&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://packshot360.wordpress.com/"&gt;&lt;span style="background: SpringGreen;"&gt;packshot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;360&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/9.aspx</link><pubDate>Wed, 12 Oct 2011 10:43:32 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:527</guid><dc:creator>smithwilsonrogard</dc:creator><description>Revision 9 posted to How to by smithwilsonrogard on 12/10/2011 07:43:32 a.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&lt;a href="http://www.essaymojo.co.uk/buy-essay.php"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;&lt;/a&gt;&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;a href="http://www.essaymojo.co.uk/write-my-essay.php"&gt;&lt;span style="background: SpringGreen;"&gt;Write&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;My&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Me&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Cur&lt;a href="http://ideal-escapes.com/"&gt;rent&lt;/a&gt;ly I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.  &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
Hill University Accreditation&lt;/a&gt; AND &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
About Woodfield University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;a href="http://www.essayinn.co.uk"&gt;&lt;span style="background: SpringGreen;"&gt;UK&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate. &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;Rochville
 University&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
Adisonhighschool&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
Corllins University Review&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;a href="http://www.essaymojo.co.uk/essay-service.php"&gt;&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Service&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&lt;a href="http://www.mightydesigners.com"&gt;&lt;span style="background: SpringGreen;"&gt;Logo&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Contests&lt;/span&gt;&lt;/a&gt;&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/8.aspx</link><pubDate>Wed, 06 Apr 2011 16:58:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:520</guid><dc:creator>suedallin</dc:creator><description>Revision 8 posted to How to by suedallin on 06/04/2011 01:58:15 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Currently&lt;/span&gt; &lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Cur&lt;/span&gt;&lt;a href="http://ideal-escapes.com/"&gt;&lt;span style="background: SpringGreen;"&gt;rent&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;ly&lt;/span&gt; I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.  &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
Hill University Accreditation&lt;/a&gt; AND &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
About Woodfield University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate. &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;Rochville
 University&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
Adisonhighschool&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
Corllins University Review&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/7.aspx</link><pubDate>Wed, 30 Mar 2011 11:46:10 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:389</guid><dc:creator>markweee</dc:creator><description>Revision 7 posted to How to by markweee on 30/03/2011 08:46:10 a.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.  &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Hill&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;About&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Woodfield&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate. &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;Rochville&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Adisonhighschool&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Corllins&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Review&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/6.aspx</link><pubDate>Sat, 03 Apr 2010 18:14:59 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:383</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 6 posted to How to by Chris Nicola on 03/04/2010 03:14:59 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/5.aspx</link><pubDate>Fri, 25 Dec 2009 00:31:36 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:313</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 5 posted to How to by Chris Nicola on 24/12/2009 09:31:36 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;DDL&lt;/span&gt; to generate the table &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;store&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;next&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;id&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt; for &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;my&lt;/span&gt; custom id is
only &lt;span style="text-decoration: line-through; color: red;"&gt;run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;created&lt;/span&gt; once &lt;span style="text-decoration: line-through; color: red;"&gt;per&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;and &lt;span style="text-decoration: line-through; color: red;"&gt;then&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;does&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; the &lt;span style="text-decoration: line-through; color: red;"&gt;base&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; class&lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;inheriting)&lt;/span&gt;.&amp;nbsp; For my id generator there is a separate column used for
each table in the database&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;So&lt;/span&gt;, &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;so&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; example&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/i&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;getting&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;appears&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;&lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hilo&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SchemaExport&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;currently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;per&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/4.aspx</link><pubDate>Fri, 25 Dec 2009 00:25:51 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:304</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 4 posted to How to by Chris Nicola on 24/12/2009 09:25:51 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/3.aspx</link><pubDate>Fri, 25 Dec 2009 00:18:06 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:303</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 3 posted to How to by Chris Nicola on 24/12/2009 09:18:06 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/2.aspx</link><pubDate>Thu, 03 Sep 2009 16:42:06 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:302</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 2 posted to How to by Chris Nicola on 03/09/2009 01:42:06 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/15/generate-custom-ddl-for-your-custom-id-generator-with-nhibernate"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--{12519950541020}--&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--{12519956184740}--&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/1.aspx</link><pubDate>Thu, 03 Sep 2009 16:41:21 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:293</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 1 posted to How to by Chris Nicola on 03/09/2009 01:41:21 p.m.&lt;br /&gt;
&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/15/generate-custom-ddl-for-your-custom-id-generator-with-nhibernate"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&amp;lt;!--{12519950541020}--&amp;gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;&amp;lt;!--{12519956184740}--&amp;gt;&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Checking if an Unloaded Collection Contains Elements</title><link>http://nhforge.org/wikis/howtonh/checking-if-an-unloaded-collection-contains-elements/revision/0.aspx</link><pubDate>Fri, 18 Nov 2011 15:35:52 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:95</guid><dc:creator>Ricardo Peres</dc:creator><description>Current revision posted to How to by Ricardo Peres on 18/11/2011 12:35:52 p.m.&lt;br /&gt;
&lt;p&gt;If you want to know if an unloaded collection in an entity contains elements, or count them, without actually loading them, you need to use a custom query; that is because the&amp;nbsp;&lt;strong&gt;Count&amp;nbsp;&lt;/strong&gt;property (if the collection is not mapped with&amp;nbsp;&lt;strong&gt;lazy=&amp;rdquo;extra&amp;rdquo;&lt;/strong&gt;) and the LINQ&amp;nbsp;&lt;strong&gt;Count()&lt;/strong&gt;&amp;nbsp;and&lt;strong&gt;Any()&lt;/strong&gt;&amp;nbsp;methods force the whole collection to be loaded.&lt;/p&gt;
&lt;p&gt;You can use something like these two methods, one for checking if there are any values, the other for actually counting them:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; Boolean Exists(&lt;span&gt;this&lt;/span&gt; ISession session, IEnumerable collection)&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre&gt;     &lt;span&gt;if&lt;/span&gt; (collection &lt;span&gt;is&lt;/span&gt; IPersistentCollection)&lt;/pre&gt;
&lt;pre&gt;     {&lt;/pre&gt;
&lt;pre&gt;         IPersistentCollection col = collection &lt;span&gt;as&lt;/span&gt; IPersistentCollection;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;         &lt;span&gt;if&lt;/span&gt; (col.WasInitialized == &lt;span&gt;false&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;         {&lt;/pre&gt;
&lt;pre&gt;                 String[] roleParts = col.Role.Split(&lt;span&gt;&amp;#39;.&amp;#39;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;                 String ownerTypeName = String.Join(&lt;span&gt;&amp;quot;.&amp;quot;&lt;/span&gt;, roleParts, 0, roleParts.Length - 1);&lt;/pre&gt;
&lt;pre&gt;                 String ownerCollectionName = roleParts.Last();&lt;/pre&gt;
&lt;pre&gt;                 String hql = &lt;span&gt;&amp;quot;select 1 from &amp;quot;&lt;/span&gt; + ownerTypeName + &lt;span&gt;&amp;quot; it where it.id = :id and exists elements(it.&amp;quot;&lt;/span&gt; + ownerCollectionName + &lt;span&gt;&amp;quot;)&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;                 Boolean exists = session.CreateQuery(hql).SetParameter(&lt;span&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, col.Key).List().Count == 1;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;                 &lt;span&gt;return&lt;/span&gt; (exists);&lt;/pre&gt;
&lt;pre&gt;         }&lt;/pre&gt;
&lt;pre&gt;     }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;     &lt;span&gt;return&lt;/span&gt; ((collection &lt;span&gt;as&lt;/span&gt; IEnumerable).OfType&amp;lt;Object&amp;gt;().Any());&lt;/pre&gt;
&lt;pre&gt; }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; Int64 Count(&lt;span&gt;this&lt;/span&gt; ISession session, IEnumerable collection)&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre&gt;    &lt;span&gt;if&lt;/span&gt; (collection &lt;span&gt;is&lt;/span&gt; IPersistentCollection)&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre&gt;        IPersistentCollection col = collection &lt;span&gt;as&lt;/span&gt; IPersistentCollection;&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre&gt;        &lt;span&gt;if&lt;/span&gt; (col.WasInitialized == &lt;span&gt;false&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre&gt;            String[] roleParts = col.Role.Split(&lt;span&gt;&amp;#39;.&amp;#39;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;            String ownerTypeName = String.Join(&lt;span&gt;&amp;quot;.&amp;quot;&lt;/span&gt;, roleParts, 0, roleParts.Length - 1);&lt;/pre&gt;
&lt;pre&gt;            String ownerCollectionName = roleParts.Last();&lt;/pre&gt;
&lt;pre&gt;            String hql = &lt;span&gt;&amp;quot;select count(elements(it.&amp;quot;&lt;/span&gt; + ownerCollectionName + &lt;span&gt;&amp;quot;)) from &amp;quot;&lt;/span&gt; + ownerTypeName + &lt;span&gt;&amp;quot; it where it.id = :id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;            Int64 count = session.CreateQuery(hql).SetParameter(&lt;span&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, col.Key).UniqueResult&amp;lt;Int64&amp;gt;();&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre&gt;            &lt;span&gt;return&lt;/span&gt; (count);&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre&gt;    &lt;span&gt;return&lt;/span&gt; ((collection &lt;span&gt;as&lt;/span&gt; IEnumerable).OfType&amp;lt;Object&amp;gt;().Count());&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Here&amp;#39;s how:&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;MyEntity entity = session.Load(100);&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;if (session.Exists(entity.SomeCollection))&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre&gt;    Int32 count = session.Count(entity.SomeCollection);&lt;/pre&gt;
&lt;pre&gt;    //...&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/0.aspx</link><pubDate>Sat, 29 Oct 2011 01:07:45 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:73</guid><dc:creator>brendan2452</dc:creator><description>Current revision posted to How to by brendan2452 on 28/10/2011 10:07:45 p.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;IUserType&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Filters&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;l18n&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;localization&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on a post in spanish on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a Fabio &lt;span style="text-decoration: line-through; color: red;"&gt;Maulo&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;`&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Maulo`s&lt;/span&gt; post called Localized Property with NHibernate, in which he discusses the use of the LocalizablePropertyType found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: Localizing NHibernate: Contextual Parameters by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: Create a multi languaged domain model with NHibernate and C# by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: Mapping translations in NHibernate by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: Localizable entities with Nhibernate part 1 (part 2 and part 3) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;[howtonh:stuffynoseremedy.net/stuffy-nose-remedy/|stuffy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;nose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;remedy]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt; mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using NHibernate.Search&amp;nbsp;and Lucene.NET using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in NHUsers discussion on the topic.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/13.aspx</link><pubDate>Fri, 28 Oct 2011 14:13:21 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:523</guid><dc:creator>John Davidson</dc:creator><description>Revision 13 posted to How to by John Davidson on 28/10/2011 11:13:21 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;a href="http://www.casinomagic.co.uk/%20%20%20"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Best&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Casino&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/12.aspx</link><pubDate>Fri, 28 Oct 2011 13:56:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:522</guid><dc:creator>liliasmit</dc:creator><description>Revision 12 posted to How to by liliasmit on 28/10/2011 10:56:54 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database. &lt;a href="http://www.casinomagic.co.uk/%20%20%20"&gt;&lt;span style="background: SpringGreen;"&gt;Best&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Casino&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/11.aspx</link><pubDate>Wed, 06 Jul 2011 13:22:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:521</guid><dc:creator>John Davidson</dc:creator><description>Revision 11 posted to How to by John Davidson on 06/07/2011 10:22:53 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This&lt;a href="http://www.regentmbaprojects.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;MBA&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reports&lt;/span&gt;&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping&lt;a href="http://hfiainsurance.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;auto&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;insurance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;quotes&lt;/span&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/10.aspx</link><pubDate>Wed, 06 Jul 2011 04:59:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:486</guid><dc:creator>MBA Project reports</dc:creator><description>Revision 10 posted to How to by MBA Project reports on 06/07/2011 01:59:56 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This &lt;a href="http://www.regentmbaprojects.com/"&gt;&lt;span style="background: SpringGreen;"&gt;MBA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reports&lt;/span&gt;&lt;/a&gt; second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/9.aspx</link><pubDate>Fri, 01 Jul 2011 10:41:47 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:485</guid><dc:creator>John Davidson</dc:creator><description>Revision 9 posted to How to by John Davidson on 01/07/2011 07:41:47 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp; &lt;a href="http://www.cjmdefense.com/dui-lawyer-huntington-beach"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;DUI&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Lawyer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;huntington&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Beach&lt;/span&gt;&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/8.aspx</link><pubDate>Fri, 01 Jul 2011 10:41:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:470</guid><dc:creator>John Davidson</dc:creator><description>Revision 8 posted to How to by John Davidson on 01/07/2011 07:41:12 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;|&lt;/span&gt;
&lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Must&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Must&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Accredited&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;|&lt;/span&gt; &lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;|&lt;/span&gt; &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Must&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;LCD&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HDTV&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp; &lt;a href="http://www.cjmdefense.com/dui-lawyer-huntington-beach"&gt;DUI Lawyer in huntington Beach&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/7.aspx</link><pubDate>Fri, 01 Jul 2011 07:43:11 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:469</guid><dc:creator>Criminal Defense Lawyer in Long Beach</dc:creator><description>Revision 7 posted to How to by Criminal Defense Lawyer in Long Beach on 01/07/2011 04:43:11 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;Mustuniversity&lt;/a&gt; |
&lt;a href="http://www.aboutmustuniversity.com/"&gt;Must
University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;Must University
Accredited&lt;/a&gt; | &lt;a href="http://www.aboutmustuniversity.com/"&gt;Mustuniversity&lt;/a&gt;
 | &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;Must
University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;LCD HDTV&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp; &lt;a href="http://www.cjmdefense.com/dui-lawyer-huntington-beach"&gt;&lt;span style="background: SpringGreen;"&gt;DUI&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lawyer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;huntington&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Beach&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/6.aspx</link><pubDate>Wed, 30 Mar 2011 11:47:46 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:468</guid><dc:creator>markweee</dc:creator><description>Revision 6 posted to How to by markweee on 30/03/2011 08:47:46 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;|&lt;/span&gt;
&lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Must&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Accredited&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;|&lt;/span&gt; &lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="background: SpringGreen;"&gt;|&lt;/span&gt; &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Must&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;LCD HDTV&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/5.aspx</link><pubDate>Fri, 24 Sep 2010 17:18:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:384</guid><dc:creator>mikeon098</dc:creator><description>Revision 5 posted to How to by mikeon098 on 24/09/2010 02:18:41 p.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;LCD HDTV&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/4.aspx</link><pubDate>Thu, 26 Aug 2010 12:35:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:348</guid><dc:creator>tcook23</dc:creator><description>Revision 4 posted to How to by tcook23 on 26/08/2010 09:35:37 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;&lt;span style="background: SpringGreen;"&gt;LCD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HDTV&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://zolpo.com/auto-insurance/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/3.aspx</link><pubDate>Fri, 28 May 2010 12:41:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:343</guid><dc:creator>mike3050</dc:creator><description>Revision 3 posted to How to by mike3050 on 28/05/2010 09:41:41 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://zolpo.com/auto-insurance/"&gt;&lt;span style="background: SpringGreen;"&gt;auto&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;insurance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;quotes&lt;/span&gt;&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/2.aspx</link><pubDate>Tue, 16 Mar 2010 15:27:42 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:317</guid><dc:creator>Marc Climent</dc:creator><description>Revision 2 posted to How to by Marc Climent on 16/03/2010 12:27:42 p.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;internally)&lt;/span&gt; but we &lt;span style="background: SpringGreen;"&gt;choose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;String&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;developing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;getter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;easier&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;user&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Alternatively&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; can create &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;well&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;exposed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;through&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;getter&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;t&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/1.aspx</link><pubDate>Tue, 16 Mar 2010 12:18:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:312</guid><dc:creator>Marc Climent</dc:creator><description>Revision 1 posted to How to by Marc Climent on 16/03/2010 09:18:25 a.m.&lt;br /&gt;
&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary but we can create our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;li&gt;The entity property can&amp;#39;t be a string&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/0.aspx</link><pubDate>Mon, 19 Sep 2011 21:52:01 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:17</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 19/09/2011 06:52:01 p.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;logging&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log4net&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;assembly is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;strong&gt;show_sql&lt;/strong&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;am&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;too&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;write&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;academic&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;paper&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Thus&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;need&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;go&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;a href="http://www.exclusivepapers.com/essay-writing-service.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;service&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;custom&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;papers&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;composed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;best&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;experts&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;understand&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;lot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;people&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cannot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;become&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;successful&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;without&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assistance&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;strong&gt;Resharper&lt;/strong&gt; or be it the Output Window of Visual Studio when you use&lt;strong&gt;TestDriven&lt;/strong&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows&lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/a&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;strong&gt;Global. asax&amp;quot;&lt;/strong&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;strong&gt;root&lt;/strong&gt; section inside the &lt;strong&gt;log4net&lt;/strong&gt; node)&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;strong&gt;NHibernate&lt;/strong&gt; and &lt;strong&gt;NHibernate.SQL&lt;/strong&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;strong&gt;NHibernate&lt;/strong&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/5.aspx</link><pubDate>Mon, 19 Sep 2011 11:10:23 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:519</guid><dc:creator>barbarasmith22</dc:creator><description>Revision 5 posted to How to by barbarasmith22 on 19/09/2011 08:10:23 a.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: logging, log4net&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;assembly is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;b&gt;show_sql&lt;/b&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;am&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;too&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;write&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;academic&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;paper&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Thus&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;go&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;a href="http://www.exclusivepapers.com/essay-writing-service.php"&gt;&lt;span style="background: SpringGreen;"&gt;essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;service&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;custom&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;papers&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;composed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;best&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;experts&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;understand&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;people&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cannot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;become&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;successful&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;without&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assistance&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;b&gt;Resharper&lt;/b&gt; or be it the Output Window of Visual Studio when you use&lt;b&gt;TestDriven&lt;/b&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows &lt;a href="http://www.thesisdom.com/"&gt;Thesis Writing&lt;/a&gt;
AND &lt;a href="http://www.dissertationdom.com/"&gt;Dissertation
Writing&lt;/a&gt; AND &lt;a href="http://www.essaydom.com/"&gt;Essay
Writing&lt;/a&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;b&gt;Global. asax&amp;quot;&lt;/b&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;b&gt;root&lt;/b&gt; section inside the &lt;b&gt;log4net&lt;/b&gt; node)&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;b&gt;NHibernate&lt;/b&gt; and &lt;b&gt;NHibernate.SQL&lt;/b&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;b&gt;NHibernate&lt;/b&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/4.aspx</link><pubDate>Wed, 30 Mar 2011 11:21:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:518</guid><dc:creator>markweee</dc:creator><description>Revision 4 posted to How to by markweee on 30/03/2011 08:21:17 a.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: logging, log4net&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;assembly is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;b&gt;show_sql&lt;/b&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;b&gt;Resharper&lt;/b&gt; or be it the Output Window of Visual Studio when you use&lt;b&gt;TestDriven&lt;/b&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt;
&lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;b&gt;Global. asax&amp;quot;&lt;/b&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;b&gt;root&lt;/b&gt; section inside the &lt;b&gt;log4net&lt;/b&gt; node)&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;b&gt;NHibernate&lt;/b&gt; and &lt;b&gt;NHibernate.SQL&lt;/b&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;b&gt;NHibernate&lt;/b&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/3.aspx</link><pubDate>Fri, 17 Oct 2008 00:23:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:373</guid><dc:creator>Dario Quintana</dc:creator><description>Revision 3 posted to How to by Dario Quintana on 16/10/2008 09:23:49 p.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;logging&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;assebly&lt;/span&gt; &lt;/a&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt; is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;b&gt;show_sql&lt;/b&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;b&gt;Resharper&lt;/b&gt; or be it the Output Window of Visual Studio when you use&lt;b&gt;TestDriven&lt;/b&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;b&gt;Global. asax&amp;quot;&lt;/b&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;b&gt;root&lt;/b&gt; section inside the &lt;b&gt;log4net&lt;/b&gt; node)&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;b&gt;NHibernate&lt;/b&gt; and &lt;b&gt;NHibernate.SQL&lt;/b&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;b&gt;NHibernate&lt;/b&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/2.aspx</link><pubDate>Sun, 07 Sep 2008 17:20:36 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:184</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 2 posted to How to by Fabio Maulo on 07/09/2008 02:20:36 p.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Recently&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;there&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;question&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt;&amp;nbsp;&lt;a href="http://groups.google.com/group/nhusers?hl=en"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHUser&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;group&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;about&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;how&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;configure&lt;/span&gt;&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt;&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/strong&gt;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;queries&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;generated&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;/a&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;assebly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;distributed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; NHibernate &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;First&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&amp;nbsp;&lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Log4Net&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;There&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;binaries&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;strong&gt;show_sql&lt;/strong&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml &lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;version=&lt;/span&gt;&amp;quot;1.0&amp;quot; &lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;encoding=&lt;/span&gt;&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;show_sql&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;strong&gt;Resharper&lt;/strong&gt; or be it the Output Window of Visual Studio when you &lt;span style="text-decoration: line-through; color: red;"&gt;useTestDriven)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;TestDriven&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;. 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;..&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;configSections&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;section&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;&lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;encoding=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Others&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sections&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
      &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/configSections&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;log4net&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;log4net.Appender.ConsoleAppender,&lt;/span&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;others&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configurations&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contains&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; log4net &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;layout&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;log4net.Layout.PatternLayout,&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;settings&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;debug&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;output&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;appenders&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Appender.TraceAppender,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;ConversionPattern&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;%d&lt;/span&gt;
&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
             &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;%d{ABSOLUTE}&lt;/span&gt; %&lt;span style="text-decoration: line-through; color: red;"&gt;p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-5p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;c{1}:%L&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;RollingFile&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;console&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Appender.ConsoleAppender,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
             &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;%d{ABSOLUTE}&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-5p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;c{1}:%L&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;m%n&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;File&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;AppendToFile&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;DatePattern&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;layout&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;conversionPattern&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;%d &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;%t]&lt;/span&gt; %&lt;span style="text-decoration: line-through; color: red;"&gt;p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-5p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;c&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;root&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;priority&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Setup&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;category&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;appenders&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;default&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;priority&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;priority&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender-ref&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;ref&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;console&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/root&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;..&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;strong&gt;Global. asax&amp;quot;&lt;/strong&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;
[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="text-decoration: line-through; color: red;"&gt;true)&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;2008-07-02 08:40:07,249 INFO NHibernate &lt;span style="text-decoration: line-through; color: red;"&gt;2.0.0.1001&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;2.0.0.4000&lt;/span&gt; (&lt;span style="text-decoration: line-through; color: red;"&gt;2.0.0.1001)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;2.0.0.4000)&lt;/span&gt;
2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file
2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg
2008-07-02 08:40:07,257 INFO Using reflection optimizer
2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider
2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect
2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver
2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;
2008-07-02 08:40:08,518 DEBUG show_sql=false
2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]
2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml
2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect
2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person
2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32
2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String
2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String
2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime
2008-07-02 08:40:11,632 INFO checking mappings queue
2008-07-02 08:40:11,634 INFO processing one-to-many association mappings
2008-07-02 08:40:11,634 INFO processing one-to-one association property references
2008-07-02 08:40:11,634 INFO processing foreign key constraints
2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect
2008-07-02 08:40:11,716 INFO Using dialect defined converter
...
2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person
2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?
2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?
...
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;strong&gt;root&lt;/strong&gt; section inside the &lt;strong&gt;log4net&lt;/strong&gt; node)&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;logger&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;NHibernate&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;additivity=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;false&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;level&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;WARN&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;RollingFile&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;level&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/logger&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;logger&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;NHibernate.SQL&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;additivity=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;false&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;level&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ALL&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;RollingFile&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;level&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/logger&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;strong&gt;NHibernate&lt;/strong&gt; and &lt;strong&gt;NHibernate.SQL&lt;/strong&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, 
                        person0_.LastName as LastName0_, 
                        person0_.FirstName as FirstName0_, 
                        person0_.Birthdate as Birthdate0_ 
                        from Person person0_
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;strong&gt;NHibernate&lt;/strong&gt; filter to say INFO or DEBUG&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;As&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;usual&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demo&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt;&amp;nbsp;&lt;a href="http://hibernatingrhinos.googlecode.com/svn/trunk/Logging"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt;.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/1.aspx</link><pubDate>Sun, 07 Sep 2008 05:09:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:51</guid><dc:creator>gabriel.schenker</dc:creator><description>Revision 1 posted to How to by gabriel.schenker on 07/09/2008 02:09:37 a.m.&lt;br /&gt;
&lt;p&gt;Recently there was a
question in the&amp;nbsp;&lt;a href="http://groups.google.com/group/nhusers?hl=en"&gt;NHUser&lt;/a&gt;&amp;nbsp;group about how to
configure&amp;nbsp;&lt;strong&gt;Log4Net&lt;/strong&gt;&amp;nbsp;to log the queries
generated by NHibernate. First of all: you can get&amp;nbsp;&lt;strong&gt;Log4Net&lt;/strong&gt;&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;here&lt;/a&gt;. There you also find
the documentation. If you are not sure about details of the configuration shown
below please refer to&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt;&amp;nbsp;documentation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Running without
Log4Net&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you only want
NHibernate to log the queries it sends to the data source when running unit
tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the&amp;nbsp;&lt;strong&gt;show_sql&lt;/strong&gt;&amp;nbsp;key to the NHibernate
configuration. If you are using a separate xml file to configure NHibernate
(e.g. the hibernate.cfg.xml) then its content might look similar to the one
below&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&amp;lt;?&lt;/span&gt;&lt;span&gt;xml&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;version&lt;/span&gt;&lt;span&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;encoding&lt;/span&gt;&lt;span&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span&gt; ?&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;hibernate-configuration&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;xmlns&lt;/span&gt;&lt;span&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;session-factory&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;show_sql&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;true&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;session-factory&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;hibernate-configuration&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;Now when running any
unit test that involves NHibernate the queries generated by NHibernate will be
logged in the output window (be it the unit test runner of&amp;nbsp;Resharper&amp;nbsp;or be it the Output
Window of Visual Studio when you useTestDriven).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Logging with Log4Net&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you want to use
Log4Net to collect logging information generated by NHibernate you have to add
the necessary configuration to the config file of your application. If you want
to log to two different targets (e.g. to the console and to a file) the relevant
sections in your config file might look like follows&lt;/p&gt;
&lt;p&gt;&amp;lt;configuration&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;
...&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;configSections&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;section name=&amp;quot;log4net&amp;quot; type=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;/configSections&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;log4net&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;appender name=&amp;quot;ConsoleAppender&amp;quot; type=&amp;quot;log4net.Appender.ConsoleAppender,
log4net&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;layout type=&amp;quot;log4net.Layout.PatternLayout,
log4net&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;ConversionPattern&amp;quot; value=&amp;quot;%d
%p %m%n&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;/layout&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/appender&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;appender name=&amp;quot;RollingFile&amp;quot; type=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot; &amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;File&amp;quot; value=&amp;quot;log.txt&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;AppendToFile&amp;quot; value=&amp;quot;true&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;DatePattern&amp;quot; value=&amp;quot;yyyy.MM.dd&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;layout type=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;conversionPattern value=&amp;quot;%d
%p %m%n&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;/layout&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/appender&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;root&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;priority value=&amp;quot;DEBUG&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;appender-ref ref=&amp;quot;ConsoleAppender&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/root&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
...&lt;/p&gt;
&lt;p&gt;&amp;lt;/configuration&amp;gt;&lt;/p&gt;
&lt;p&gt;Here I define that all
output with a priority of at least DEBUG goes to the console.&lt;/p&gt;
&lt;p&gt;Now I have to tell my
application that I want to use Log4Net. I can do that with an assembly level
attribute. That is, you have to put the following code snippet somewhere in
your application (e.g. the &amp;quot;&lt;strong&gt;Global. asax&amp;quot;&lt;/strong&gt;&amp;nbsp;if you are building a
web application)&lt;/p&gt;
&lt;p&gt;// Configure log4net using the .config
file&lt;/p&gt;
&lt;p&gt;[assembly:
log4net.Config.XmlConfigurator(Watch = true)]&lt;/p&gt;
&lt;p&gt;having done do we can
now run a first unit test that accesses the database via NHibernate. The output
generated by Log4Net in the unit test runner is similar to this&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,249 INFO NHibernate
2.0.0.1001 (2.0.0.1001)&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,251 INFO
hibernate-configuration section not found in application configuration file&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,254 INFO Bytecode
provider name : lcg&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,257 INFO Using
reflection optimizer&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,515 DEBUG
connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,516 DEBUG
dialect=NHibernate.Dialect.MsSql2005Dialect&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,517 DEBUG
connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,517 DEBUG
connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated
Security=SSPI;&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,522 DEBUG properties:
System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,526 INFO Mapping
resource: LoggingSample.Person.hbm.xml&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,872 INFO Using
dialect: NHibernate.Dialect.MsSql2005Dialect&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,537 DEBUG Mapped
property: Id -&amp;gt; Id, type: Int32&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,612 DEBUG Mapped
property: LastName -&amp;gt; LastName, type: String&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,614 DEBUG Mapped
property: FirstName -&amp;gt; FirstName, type: String&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,614 DEBUG Mapped
property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,632 INFO checking
mappings queue&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,634 INFO processing
one-to-many association mappings&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,634 INFO processing
one-to-one association property references&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,634 INFO processing
foreign key constraints&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,712 INFO Using
dialect: NHibernate.Dialect.MsSql2005Dialect&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,716 INFO Using dialect
defined converter&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:12,411 DEBUG&amp;nbsp; Version select: SELECT Id FROM Person WHERE
Id = ?&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:12,412 DEBUG&amp;nbsp; Snapshot select: SELECT person_.Id,
person_.LastName as
LastName0_, person_.FirstName as
FirstName0_, person_.Birthdate as
Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;as you can see LOADS
of information.&lt;/p&gt;
&lt;p&gt;To tune the generated
logging information a little bit we can filter the output generated by
NHibernate by putting the following configuration section into our config file
(put them just after the&amp;nbsp;&lt;strong&gt;root&lt;/strong&gt;&amp;nbsp;section inside the&amp;nbsp;&lt;strong&gt;log4net&lt;/strong&gt;&amp;nbsp;node)&lt;/p&gt;
&lt;p&gt;&amp;lt;logger name=&amp;quot;NHibernate&amp;quot; additivity=&amp;quot;false&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;level value=&amp;quot;WARN&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;RollingFile&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;ConsoleAppender&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/logger&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt;logger name=&amp;quot;NHibernate.SQL&amp;quot; additivity=&amp;quot;false&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;level value=&amp;quot;ALL&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;RollingFile&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;ConsoleAppender&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/logger&amp;gt;&lt;/p&gt;
&lt;p&gt;Obviously NHibernate
defines two different loggers&amp;nbsp;&lt;strong&gt;NHibernate&lt;/strong&gt;&amp;nbsp;and&amp;nbsp;&lt;strong&gt;NHibernate.SQL&lt;/strong&gt;. The first one
receives all logging output that NHibernate generates where as the second one
only receives the sql statements generated by NHibernate.&lt;/p&gt;
&lt;p&gt;With the above
settings in place the output generated is reduced to&lt;/p&gt;
&lt;p&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; person0_.LastName as LastName0_, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; person0_.FirstName as FirstName0_, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; person0_.Birthdate as Birthdate0_ &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from Person person0_&lt;/p&gt;
&lt;p&gt;That&amp;#39;s what we want.
Only if we have some weird problems we need to change the priority level of the&amp;nbsp;&lt;strong&gt;NHibernate&lt;/strong&gt;&amp;nbsp;filter to say INFO or
DEBUG.&lt;/p&gt;
&lt;p&gt;As usual you can find
a demo solution&amp;nbsp;&lt;a href="http://hibernatingrhinos.googlecode.com/svn/trunk/Logging"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:31:52 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:26</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:31:52 p.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt;&lt;/div&gt;

&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Comment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;a href="http://www.thesisdom.com/buy-thesis/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. &lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. &lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/5.aspx</link><pubDate>Wed, 30 Mar 2011 11:28:01 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:517</guid><dc:creator>markweee</dc:creator><description>Revision 5 posted to How to by markweee on 30/03/2011 08:28:01 a.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: query&lt;/div&gt;

&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Comment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3 &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory. &lt;a href="http://www.thesisdom.com/buy-thesis/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. &lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. &lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/4.aspx</link><pubDate>Thu, 18 Sep 2008 16:10:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:376</guid><dc:creator>Woil</dc:creator><description>Revision 4 posted to How to by Woil on 18/09/2008 01:10:22 p.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt;&lt;/div&gt;

&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where &lt;span style="text-decoration: line-through; color: red;"&gt;Commment.Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Comment.Name&lt;/span&gt; refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. &lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. &lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/3.aspx</link><pubDate>Fri, 12 Sep 2008 14:43:10 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:127</guid><dc:creator>Ken Egozi</dc:creator><description>Revision 3 posted to How to by Ken Egozi on 12/09/2008 11:43:10 a.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Commment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;
However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. 
&lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. 
&lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/2.aspx</link><pubDate>Fri, 12 Sep 2008 14:34:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:101</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 2 posted to How to by Fabio Maulo on 12/09/2008 11:34:49 a.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Commment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;;&lt;/span&gt;
   &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;pre&gt;Assuming the next data is in the DB:&lt;br /&gt;&amp;nbsp; Post#1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; Post#2&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; Post#3&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;&lt;br /&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;/pre&gt;
&lt;p&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. 
&lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. 
&lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Name&amp;quot;,&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;))&lt;/span&gt;
     &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
     .Add(Restrictions.EqProperty(&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Post.Id&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;p.Id&amp;quot;))&lt;/span&gt;
     &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = &lt;span style="text-decoration: line-through; color: red;"&gt;session.CreateCriteria(typeof&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.CreateCriteria&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;typeof&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;Post), &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;p&amp;quot;)&lt;/span&gt;
     &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;;&lt;/span&gt;
   &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
   var posts = session.CreateQuery(hql)
      .&lt;span style="text-decoration: line-through; color: red;"&gt;SetResultTransformer(new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SetResultTransformer(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/1.aspx</link><pubDate>Fri, 12 Sep 2008 07:32:51 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:100</guid><dc:creator>Ken Egozi</dc:creator><description>Revision 1 posted to How to by Ken Egozi on 12/09/2008 04:32:51 a.m.&lt;br /&gt;
&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;
&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;
where Commment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;pre&gt;   select  p
   from    Post p
   join    p.Comments c
   where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;;
&lt;/pre&gt;
&lt;p&gt;So, you go ahead and&lt;/p&gt;
&lt;pre&gt;   var hql = @&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;Post#1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;Post#2&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;Post#3&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;
&lt;br /&gt;
you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;/p&gt;
&lt;p&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;pre&gt;   SELECT  {p.*}
   FROM    Posts p
     JOIN  Comments c ON c.PostId = p.Id
   WHERE   c.[Name] = &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object.
Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines.
there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless.
&lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;pre&gt;   SELECT *
   FROM   Posts p
   WHERE EXISTS
   (
      SELECT 1
      FROM   Comments c
      WHERE  c.PostId = p.Id
        AND  c.[Name] = &amp;#39;Ken Egozi&amp;#39;
   )&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;
The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API.&lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;
The syntax in HQL:&lt;br /&gt;
&lt;pre&gt;   from   Post p
   where exists
   (
      from Comment c
      where c.Post = p
      and c.Name = &amp;#39;Ken Egozi&amp;#39;
   )&lt;/pre&gt;
and in Criteria API:&lt;br /&gt;
&lt;pre&gt;  var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&amp;quot;Name&amp;quot;, &amp;quot;Ken Egozi&amp;quot;))
     .Add(Restrictions.EqProperty(&amp;quot;Post.Id&amp;quot;, &amp;quot;p.Id&amp;quot;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(typeof (Post), &amp;quot;p&amp;quot;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;
That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers.
all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;
so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;pre&gt;   var hql = @&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(new DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:18:34 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:32</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:18:34 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;loading&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt;&lt;span style="color:#0000ff;"&gt;&lt;strong&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a class="ExistingPageLink" title="Click to view the page titled: Run in Medium Trust" href="/wikis/howtonh/run-in-medium-trust.aspx"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Sample Commands&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;NHPG.exe &lt;i&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/i&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;strong&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Papers&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Papers&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;strong&gt;Generate proxies with a custom Dialect &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essays&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertations&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Theses&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;/d:&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate.Dialect.Oracle9Dialect&lt;/span&gt;&lt;/i&gt; /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/12.aspx</link><pubDate>Wed, 30 Mar 2011 11:30:52 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:516</guid><dc:creator>markweee</dc:creator><description>Revision 12 posted to How to by markweee on 30/03/2011 08:30:52 a.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;b&gt;Sample Commands&lt;/b&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/b&gt;&lt;span&gt;NHPG.exe &lt;i&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/i&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Papers&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Papers&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;Generate proxies with a custom Dialect &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Essays&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Dissertations&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Theses&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;/d:&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate.Dialect.Oracle9Dialect&lt;/span&gt;&lt;/i&gt; /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/11.aspx</link><pubDate>Wed, 08 Oct 2008 18:03:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:377</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 11 posted to How to by Bill Pierce on 08/10/2008 03:03:24 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;loading&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;b&gt;Sample Commands&lt;/b&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/b&gt;&lt;span&gt;NHPG.exe &lt;i&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/i&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies&lt;br /&gt;&lt;/b&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;custom&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Dialect&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt; &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;d:&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Dialect.Oracle9Dialect&lt;/span&gt;&lt;/span&gt;&lt;/i&gt; &lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;o:Example.Domain.Proxies.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.Domain.dll&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/10.aspx</link><pubDate>Tue, 23 Sep 2008 18:22:57 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:172</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 10 posted to How to by Bill Pierce on 23/09/2008 03:22:57 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Sample Commands&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;NHPG.exe &lt;em&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/em&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;strong&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies&lt;br /&gt;&lt;/strong&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;em&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/9.aspx</link><pubDate>Tue, 23 Sep 2008 18:11:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:149</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 9 posted to How to by Bill Pierce on 23/09/2008 03:11:02 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Sample Commands&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Pre-Generate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Loading&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Proxies&lt;/span&gt; &lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Generate&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; for ActiveRecord&lt;br /&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;"&gt;NHPG.exe &lt;em&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/em&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:x-small;"&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Generate&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;multiple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assemblies&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;o:Example.Domain.Proxies.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/8.aspx</link><pubDate>Tue, 23 Sep 2008 18:09:19 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:148</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 8 posted to How to by Bill Pierce on 23/09/2008 03:09:19 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Sample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Commands&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Pre-Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ActiveRecord&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;g&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;o:Example.ActiceRecordDomain.Proxies.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/7.aspx</link><pubDate>Tue, 23 Sep 2008 03:58:50 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:147</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 7 posted to How to by Bill Pierce on 23/09/2008 12:58:50 a.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;**This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;preliminary&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documentation**&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;.&lt;span style="text-decoration: line-through; color: red;"&gt;.\..\..\lib\NHPG&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.\..\..\lib\bin\net-2.0\NHPG.exe&lt;/span&gt; /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHPG&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt;:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:x-small;"&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/6.aspx</link><pubDate>Mon, 22 Sep 2008 17:19:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:145</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 6 posted to How to by Bill Pierce on 22/09/2008 02:19:24 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for NHibernate ProxyGenerators&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\NHPG /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;done&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Pre-generated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;&lt;span style="background: SpringGreen;"&gt;Run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Medium&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Trust&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/5.aspx</link><pubDate>Mon, 22 Sep 2008 17:15:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:143</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 5 posted to How to by Bill Pierce on 22/09/2008 02:15:02 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;howtonh:/media/g/proxygenerators/default.aspx|NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; ProxyGenerators&lt;span style="text-decoration: line-through; color: red;"&gt;]&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\NHPG /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/4.aspx</link><pubDate>Mon, 22 Sep 2008 17:14:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:142</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 4 posted to How to by Bill Pierce on 22/09/2008 02:14:41 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for [howtonh:/media/g/proxygenerators/default.aspx|NHibernate &lt;span style="text-decoration: line-through; color: red;"&gt;ProxyGenerators]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ProxyGenerators&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\NHPG /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/3.aspx</link><pubDate>Mon, 22 Sep 2008 17:14:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:141</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 3 posted to How to by Bill Pierce on 22/09/2008 02:14:24 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="/media/g/burrow/default.aspx"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Proxy[[howtonh:/media/g/proxygenerators/default.aspx|NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProxyGenerators]]Generators&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;howtonh:/media/g/proxygenerators/default.aspx|NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProxyGenerators]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\NHPG /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/2.aspx</link><pubDate>Mon, 22 Sep 2008 17:13:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:140</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 2 posted to How to by Bill Pierce on 22/09/2008 02:13:56 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="/media/g/burrow/default.aspx"&gt;NHibernate &lt;span style="text-decoration: line-through; color: red;"&gt;ProxyGenerators&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Proxy[[howtonh:/media/g/proxygenerators/default.aspx|NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProxyGenerators]]Generators&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.\..\..\lib\NHPG&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;o:Example.Domain.Proxies.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.Domain.dll&lt;/span&gt;&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/1.aspx</link><pubDate>Mon, 22 Sep 2008 17:12:27 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:139</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 1 posted to How to by Bill Pierce on 22/09/2008 02:12:27 p.m.&lt;br /&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="/media/g/burrow/default.aspx"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Lazy Loaded One-To-One With NHibernate </title><link>http://nhforge.org/wikis/howtonh/lazy-loaded-one-to-one-with-nhibernate/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:13:38 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:45</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:13:38 p.m.&lt;br /&gt;
&lt;h2&gt;Lazy Loaded One-To-One With NHibernate &lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;UPDATE 20081114&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;The one-to-one solution I had posted
turned out not to work for updates, :(, in the end I had to use a
many-to-one map with a unique foreign key association to get this to
work, the updated example is below. Sorry for my EPIC FAIL :)...&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;G&amp;#39;day,&lt;/p&gt;
&lt;p&gt;I
am working on something at the moment and I am storing BLOB data for
documents in the database, I am storing things like name and data which
is a binary field. |&lt;br /&gt;Now I wan&amp;#39;t to load the binary data as loading
this all the time is very inneficient. So to get this to work I had to
split this to two tables, one with my meta data and another one-to-one&lt;br /&gt;table to store the BLOB. NOW the fun began I created the two tables:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;Documents&lt;br /&gt;-------------------&lt;br /&gt;(PK) Id&lt;br /&gt;(FK Unique) DocumentFileId&lt;br /&gt;Name&lt;br /&gt;&lt;br /&gt;DocumentFiles&lt;br /&gt;-------------------&lt;br /&gt;(PK) Id&lt;br /&gt;Data&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;From this I created my POCO classes for the Document and DocumentFile map.&lt;/p&gt;
&lt;p&gt;public class Document {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public DocumentFile DocumentFile { get; set; }&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public class DocumentFile {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual Document Document { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual byte[] Data { get; set; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Now
on to the mappings, I thought this would be as easy as created a
one-to-one mapping with lazy=&amp;quot;proxy&amp;quot; set on the one-to-one on the
Document class but this was not the case. &lt;br /&gt;You NEED to set constrained=&amp;quot;true&amp;quot; on the mapping, basically going from this post I found: &lt;a href="http://www.hibernate.org/162.html#A5"&gt;http://www.hibernate.org/162.html#A5&lt;/a&gt;. Say we have A-&amp;gt;B where this is a 1-1 relationship,&lt;br /&gt;now
without a constraint from A-B this means A can exist without B, so
there is a possiblity that B is null, a Proxy to B will be not null and
won&amp;#39;t work here.&lt;br /&gt;&lt;br /&gt;But when we know A and B will always belong together it is ok to create a Proxy for B. Now my final mapping files looks like so:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;i&gt;&lt;strong&gt;BUUUT&lt;/strong&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;strong&gt;&amp;nbsp; &lt;/strong&gt;&lt;/i&gt;as
I soon found out if in the A mapping you constrain B it will mean it
will try to insert B first and fail to generate a primary key with the
foreign key generator, hence not working as expected, I had to change
the mappings to use a many-to-one unique foreign key mapping to get
this to work. I updated the mappings below, the main differences are
highlighted below:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.Document, NHibernateDocumentTest&amp;quot; table=&amp;quot;Documents&amp;quot; lazy=&amp;quot;false&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;native&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Name&amp;quot; column=&amp;quot;Name&amp;quot; type=&amp;quot;string&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;strong&gt;&amp;nbsp; &amp;lt;many-to-one name=&amp;quot;DocumentFile&amp;quot; cascade=&amp;quot;all-delete-orphan&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lazy=&amp;quot;proxy&amp;quot; column=&amp;quot;DocumentFileId&amp;quot; unique=&amp;quot;true&amp;quot; /&amp;gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.DocumentFile, NHibernateDocumentTest&amp;quot; table=&amp;quot;DocumentFiles&amp;quot; lazy=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;strong&gt; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;native&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/id&amp;gt;&amp;nbsp; &amp;nbsp;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Data&amp;quot; column=&amp;quot;Data&amp;quot; type=&amp;quot;Byte[]&amp;quot; /&amp;gt;&lt;br /&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;one-to-one name=&amp;quot;Document&amp;quot; constrained=&amp;quot;true&amp;quot; property-ref=&amp;quot;DocumentFile&amp;quot; /&amp;gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;a href="http://www.mustuniversity.com/programs/diploma/graduate-diploma.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Graduate&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/phd-degree.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Phd&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;And
then we are done, this works fine. So the trick is for a one-to-one to
work with Lazy loading you must use a unique foreign key associated
with a many-to-one mapping to your child.&lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-certificate.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Graduate&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;Certificate&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/diploma/undergraduate-diploma.html"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Undergraduate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/schools-majors/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Schools&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks&lt;br /&gt;Stefan&lt;/p&gt;</description></item><item><title>Lazy Loaded One-To-One With NHibernate </title><link>http://nhforge.org/wikis/howtonh/lazy-loaded-one-to-one-with-nhibernate/revision/3.aspx</link><pubDate>Wed, 30 Mar 2011 11:06:31 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:515</guid><dc:creator>markweee</dc:creator><description>Revision 3 posted to How to by markweee on 30/03/2011 08:06:31 a.m.&lt;br /&gt;
&lt;h2&gt;Lazy Loaded One-To-One With NHibernate &lt;/h2&gt;
&lt;p&gt;&lt;b&gt;UPDATE 20081114&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;The one-to-one solution I had posted
turned out not to work for updates, :(, in the end I had to use a
many-to-one map with a unique foreign key association to get this to
work, the updated example is below. Sorry for my EPIC FAIL :)...&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;G&amp;#39;day,&lt;/p&gt;
&lt;p&gt;I
am working on something at the moment and I am storing BLOB data for
documents in the database, I am storing things like name and data which
is a binary field. |&lt;br /&gt;Now I wan&amp;#39;t to load the binary data as loading
this all the time is very inneficient. So to get this to work I had to
split this to two tables, one with my meta data and another one-to-one&lt;br /&gt;table to store the BLOB. NOW the fun began I created the two tables:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;Documents&lt;br /&gt;-------------------&lt;br /&gt;(PK) Id&lt;br /&gt;(FK Unique) DocumentFileId&lt;br /&gt;Name&lt;br /&gt;&lt;br /&gt;DocumentFiles&lt;br /&gt;-------------------&lt;br /&gt;(PK) Id&lt;br /&gt;Data&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;From this I created my POCO classes for the Document and DocumentFile map.&lt;/p&gt;
&lt;p&gt;public class Document {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public DocumentFile DocumentFile { get; set; }&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public class DocumentFile {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual Document Document { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual byte[] Data { get; set; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Now
on to the mappings, I thought this would be as easy as created a
one-to-one mapping with lazy=&amp;quot;proxy&amp;quot; set on the one-to-one on the
Document class but this was not the case. &lt;br /&gt;You NEED to set constrained=&amp;quot;true&amp;quot; on the mapping, basically going from this post I found: &lt;a href="http://www.hibernate.org/162.html#A5"&gt;http://www.hibernate.org/162.html#A5&lt;/a&gt;. Say we have A-&amp;gt;B where this is a 1-1 relationship,&lt;br /&gt;now
without a constraint from A-B this means A can exist without B, so
there is a possiblity that B is null, a Proxy to B will be not null and
won&amp;#39;t work here.&lt;br /&gt;&lt;br /&gt;But when we know A and B will always belong together it is ok to create a Proxy for B. Now my final mapping files looks like so:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;i&gt;&lt;b&gt;BUUUT&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;b&gt;&amp;nbsp; &lt;/b&gt;&lt;/i&gt;as
I soon found out if in the A mapping you constrain B it will mean it
will try to insert B first and fail to generate a primary key with the
foreign key generator, hence not working as expected, I had to change
the mappings to use a many-to-one unique foreign key mapping to get
this to work. I updated the mappings below, the main differences are
highlighted below:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.Document, NHibernateDocumentTest&amp;quot; table=&amp;quot;Documents&amp;quot; lazy=&amp;quot;false&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;native&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Name&amp;quot; column=&amp;quot;Name&amp;quot; type=&amp;quot;string&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt;&amp;nbsp; &amp;lt;many-to-one name=&amp;quot;DocumentFile&amp;quot; cascade=&amp;quot;all-delete-orphan&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lazy=&amp;quot;proxy&amp;quot; column=&amp;quot;DocumentFileId&amp;quot; unique=&amp;quot;true&amp;quot; /&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.DocumentFile, NHibernateDocumentTest&amp;quot; table=&amp;quot;DocumentFiles&amp;quot; lazy=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;native&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/id&amp;gt;&amp;nbsp; &amp;nbsp;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Data&amp;quot; column=&amp;quot;Data&amp;quot; type=&amp;quot;Byte[]&amp;quot; /&amp;gt;&lt;br /&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;one-to-one name=&amp;quot;Document&amp;quot; constrained=&amp;quot;true&amp;quot; property-ref=&amp;quot;DocumentFile&amp;quot; /&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt; &lt;a href="http://www.mustuniversity.com/programs/diploma/graduate-diploma.html"&gt;&lt;span style="background: SpringGreen;"&gt;Graduate&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/phd-degree.html"&gt;&lt;span style="background: SpringGreen;"&gt;Phd&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;And
then we are done, this works fine. So the trick is for a one-to-one to
work with Lazy loading you must use a unique foreign key associated
with a many-to-one mapping to your child. &lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-certificate.html"&gt;&lt;span style="background: SpringGreen;"&gt;Graduate&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Certificate&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/diploma/undergraduate-diploma.html"&gt;
&lt;span style="background: SpringGreen;"&gt;Undergraduate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/schools-majors/"&gt;&lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Schools&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks&lt;br /&gt;Stefan&lt;/p&gt;</description></item><item><title>Lazy Loaded One-To-One With NHibernate </title><link>http://nhforge.org/wikis/howtonh/lazy-loaded-one-to-one-with-nhibernate/revision/2.aspx</link><pubDate>Fri, 14 Nov 2008 01:57:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:369</guid><dc:creator>stefan.sedich</dc:creator><description>Revision 2 posted to How to by stefan.sedich on 13/11/2008 10:57:49 p.m.&lt;br /&gt;
&lt;h2&gt;Lazy Loaded One-To-One With NHibernate &lt;/h2&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;UPDATE&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;20081114&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one-to-one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;had&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;posted&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;turned&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;out&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;work&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updates&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;end&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;had&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;many-to-one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;map&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unique&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;foreign&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;association&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;work&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;below&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Sorry&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;my&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;EPIC&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FAIL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)...&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;G&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;day&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I
am working on something at the moment and I am storing BLOB data for
documents in the database, I am storing things like name and data which
is a binary field. |&lt;br /&gt;Now I wan&amp;#39;t to load the binary data as loading
this all the time is very inneficient. So to get this to work I had to
split this to two tables, one with my meta data and another one-to-one&lt;br /&gt;table to store the BLOB. NOW the fun began I created the two tables:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;Documents&lt;br /&gt;-------------------&lt;br /&gt;(PK) Id&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;FK&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Unique&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;DocumentFileId&lt;/span&gt;&lt;br /&gt;Name&lt;br /&gt;&lt;br /&gt;DocumentFiles&lt;br /&gt;-------------------&lt;br /&gt;(&lt;span style="text-decoration: line-through; color: red;"&gt;FK)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PK)&lt;/span&gt; Id&lt;br /&gt;Data&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;From this I created my POCO classes for the Document and DocumentFile map.&lt;/p&gt;
&lt;p&gt;public class Document {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public DocumentFile DocumentFile { get; set; }&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public class DocumentFile {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual Document Document { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual byte[] Data { get; set; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Now
on to the mappings, I thought this would be as easy as created a
one-to-one mapping with lazy=&amp;quot;proxy&amp;quot; set on the one-to-one on the
Document class but this was not the case. &lt;br /&gt;You NEED to set constrained=&amp;quot;true&amp;quot; on the mapping, basically going from this post I found: &lt;a href="http://www.hibernate.org/162.html#A5"&gt;http://www.hibernate.org/162.html#A5&lt;/a&gt;. Say we have A-&amp;gt;B where this is a 1-1 relationship,&lt;br /&gt;now
without a constraint from A-B this means A can exist without B, so
there is a possiblity that B is null, a Proxy to B will be not null and
won&amp;#39;t work here.&lt;br /&gt;&lt;br /&gt;But when we know A and B will always belong together it is ok to create a Proxy for B. Now my final mapping files looks like so:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;i&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;BUUUT&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;b&gt;&amp;nbsp; &lt;/b&gt;&lt;/i&gt;&lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;soon&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;out&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;A&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;constrain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;B&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mean&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;try&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;insert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;B&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fail&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;primary&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;foreign&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;generator&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hence&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expected&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;had&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mappings&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;many-to-one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unique&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;foreign&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;work&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mappings&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;below&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;main&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;differences&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;highlighted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;below&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.Document, NHibernateDocumentTest&amp;quot; table=&amp;quot;Documents&amp;quot; lazy=&amp;quot;false&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;native&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Name&amp;quot; column=&amp;quot;Name&amp;quot; type=&amp;quot;string&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;one-to-one&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;many-to-one&lt;/span&gt; name=&amp;quot;DocumentFile&amp;quot; cascade=&amp;quot;all-delete-orphan&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lazy=&amp;quot;proxy&amp;quot; &lt;span style="text-decoration: line-through; color: red;"&gt;constrained=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;column=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;DocumentFileId&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unique=&lt;/span&gt;&amp;quot;true&amp;quot; /&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.DocumentFile, NHibernateDocumentTest&amp;quot; table=&amp;quot;DocumentFiles&amp;quot; lazy=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;foreign&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;property&amp;quot;&amp;gt;Document&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/generator&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/id&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;native&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;/id&amp;gt;&amp;nbsp;&lt;/span&gt; &amp;nbsp;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Data&amp;quot; column=&amp;quot;Data&amp;quot; type=&amp;quot;Byte[]&amp;quot; /&amp;gt;&lt;br /&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;one-to-one name=&amp;quot;Document&amp;quot; &lt;span style="text-decoration: line-through; color: red;"&gt;fetch=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;select&amp;quot;&lt;/span&gt; constrained=&amp;quot;true&amp;quot; &lt;span style="background: SpringGreen;"&gt;property-ref=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;DocumentFile&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Basically&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one-to-one&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Document&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;has&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contraint&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;DocumentFile&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;so&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;these&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;always&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;belong&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;together&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;case&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Proxy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one-to-one&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;And
then we are done, this works fine. So the trick is for a one-to-one to
work with Lazy loading you must &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; a &lt;span style="text-decoration: line-through; color: red;"&gt;constraint&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;relationship&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;done&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unique&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;foreign&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;associated&lt;/span&gt;
with a &lt;span style="text-decoration: line-through; color: red;"&gt;Many-To-One&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;where&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;case&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;where&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;A&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;exist&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;without&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;B&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;many-to-one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;child&lt;/span&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks&lt;br /&gt;Stefan&lt;/p&gt;</description></item><item><title>Lazy Loaded One-To-One With NHibernate </title><link>http://nhforge.org/wikis/howtonh/lazy-loaded-one-to-one-with-nhibernate/revision/1.aspx</link><pubDate>Thu, 13 Nov 2008 14:15:11 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:193</guid><dc:creator>stefan.sedich</dc:creator><description>Revision 1 posted to How to by stefan.sedich on 13/11/2008 11:15:11 a.m.&lt;br /&gt;
&lt;p&gt;I am working on something at the moment and I am storing BLOB data
for documents in the database, I am storing things like name and data
which is a binary field. |&lt;br /&gt;Now I wan&amp;#39;t to load the binary data as
loading this all the time is very inneficient. So to get this to work I
had to split this to two tables, one with my meta data and another
one-to-one&lt;br /&gt;table to store the BLOB. NOW the fun began I created the two tables:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;Documents&lt;br /&gt;-------------------&lt;br /&gt;(PK) Id&lt;br /&gt;Name&lt;br /&gt;&lt;br /&gt;DocumentFiles&lt;br /&gt;-------------------&lt;br /&gt;(FK) Id&lt;br /&gt;Data&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;From this I created my POCO classes for the Document and DocumentFile map.&lt;/p&gt;
&lt;p&gt;public class Document {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public DocumentFile DocumentFile { get; set; }&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public class DocumentFile {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual int Id { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual Document Document { get; set; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public virtual byte[] Data { get; set; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Now
on to the mappings, I thought this would be as easy as created a
one-to-one mapping with lazy=&amp;quot;proxy&amp;quot; set on the one-to-one on the
Document class but this was not the case. &lt;br /&gt;You NEED to set constrained=&amp;quot;true&amp;quot; on the mapping, basically going from this post I found: &lt;a href="http://www.hibernate.org/162.html#A5"&gt;http://www.hibernate.org/162.html#A5&lt;/a&gt;. Say we have A-&amp;gt;B where this is a 1-1 relationship,&lt;br /&gt;now
without a constraint from A-B this means A can exist without B, so
there is a possiblity that B is null, a Proxy to B will be not null and
won&amp;#39;t work here.&lt;br /&gt;&lt;br /&gt;But when we know A and B will always belong together it is ok to create a Proxy for B. Now my final mapping files looks like so:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.Document, NHibernateDocumentTest&amp;quot; table=&amp;quot;Documents&amp;quot; lazy=&amp;quot;false&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;native&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Name&amp;quot; column=&amp;quot;Name&amp;quot; type=&amp;quot;string&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;one-to-one name=&amp;quot;DocumentFile&amp;quot; cascade=&amp;quot;all-delete-orphan&amp;quot; lazy=&amp;quot;proxy&amp;quot; constrained=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt; &lt;br /&gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;NHibernateDocumentTest.DocumentFile, NHibernateDocumentTest&amp;quot; table=&amp;quot;DocumentFiles&amp;quot; lazy=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; type=&amp;quot;integer&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;foreign&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name=&amp;quot;property&amp;quot;&amp;gt;Document&amp;lt;/param&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/generator&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/id&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Data&amp;quot; column=&amp;quot;Data&amp;quot; type=&amp;quot;Byte[]&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;one-to-one name=&amp;quot;Document&amp;quot; fetch=&amp;quot;select&amp;quot; constrained=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;br /&gt;Basically
the one-to-one in the Document has contraint for DocumentFile so these
always belong together as is the case here, also I have set lazy to
Proxy for this one-to-one. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And then we are done, this
works fine. So the trick is for a one-to-one to work with Lazy loading
you must have a constraint on your relationship. &lt;br /&gt;This can also be done with a Many-To-One where it is the case where A can exist without B.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks&lt;br /&gt;Stefan&lt;/p&gt;</description></item><item><title>Dynamically change user info in connection string</title><link>http://nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:09:08 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:46</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:09:08 p.m.&lt;br /&gt;
&lt;h2&gt;Dynamically change user info in connection string&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;QuickStart&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;In some cases our clients has to use the same database
user id in each connection, so they can use audit and security features of
their database system (and their DBAs will be happy [:)]).&lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp" rel="follow"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Hill&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp" rel="follow"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Woodfield&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;To do that in Nh We can use the ConnectionProvider
facility. Just derive a class from the&amp;nbsp; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp" rel="follow"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Corllins&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&amp;nbsp;standard&amp;nbsp;DriverConnectionProvider
class:&lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp" rel="follow"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Ashwood&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp" rel="follow"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Rochville&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DynamicConnectionProvider &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;DriverConnectionProvider
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private string &lt;/span&gt;_connectionString;
    &lt;span style="color:blue;"&gt;public override void &lt;/span&gt;Configure(&lt;span style="color:#2b91af;"&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt; settings)
    {

        &lt;span style="color:green;"&gt;// Connection string in the configuration overrides named connection string
        &lt;/span&gt;&lt;span style="color:blue;"&gt;if &lt;/span&gt;(!settings.TryGetValue(NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionString,&lt;span style="color:blue;"&gt;out &lt;/span&gt;_connectionString))
    _connectionString = GetNamedConnectionString(settings);

        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(_connectionString == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:blue;"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;HibernateException&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Could not find connection string setting (set &amp;quot; 
                &lt;/span&gt;+ NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionString + &lt;span style="color:#a31515;"&gt;&amp;quot; or &amp;quot; 
                &lt;/span&gt;+ NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionStringName + &lt;span style="color:#a31515;"&gt;&amp;quot; property)&amp;quot;&lt;/span&gt;);
        }
        ConfigureDriver(settings);
    }&lt;/pre&gt;
&lt;p&gt;This is necessary because the original connection
string is private, but just copy the code from base method.&lt;/p&gt;
&lt;p&gt;The real magic is in &amp;quot;ConnectionString&amp;quot;
property, it is called when nh has to connect in a Session. You have to
override it so you can make the changes you need.&lt;span style="white-space:pre;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;protected override string &lt;/span&gt;ConnectionString
{
    &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;FixConnectionString(_connectionString); }
}
&lt;/pre&gt;
&lt;p&gt;In this case &lt;i&gt;FixConnectionString&lt;/i&gt;&amp;nbsp;read
the user info from some environment variable and inject it in the connection
string.&lt;/p&gt;
&lt;p&gt;Finally configure NH to use the
ConnectionProvider:&lt;/p&gt;
&lt;pre class="code"&gt;            &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;connection.provider&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;&amp;gt;
                &lt;/span&gt;MyAssembly.DynamicConnectionProvider, MyAssembly
            &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Some useful articles&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;&lt;a href="http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html" style="text-decoration:none;"&gt;here&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx"&gt;here&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Dynamically change user info in connection string</title><link>http://nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string/revision/5.aspx</link><pubDate>Wed, 30 Mar 2011 11:34:18 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:514</guid><dc:creator>markweee</dc:creator><description>Revision 5 posted to How to by markweee on 30/03/2011 08:34:18 a.m.&lt;br /&gt;
&lt;h2&gt;Dynamically change user info in connection string&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, QuickStart&lt;/div&gt;

&lt;p&gt;In some cases our clients has to use the same database
user id in each connection, so they can use audit and security features of
their database system (and their DBAs will be happy [:)]). &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Hill&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Woodfield&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To do that in Nh We can use the ConnectionProvider
facility. Just derive a class from the&amp;nbsp; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Corllins&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;standard&amp;nbsp;DriverConnectionProvider
class: &lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Ashwood&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Rochville&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DynamicConnectionProvider &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;DriverConnectionProvider
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private string &lt;/span&gt;_connectionString;
    &lt;span style="color:blue;"&gt;public override void &lt;/span&gt;Configure(&lt;span style="color:#2b91af;"&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt; settings)
    {

        &lt;span style="color:green;"&gt;// Connection string in the configuration overrides named connection string
        &lt;/span&gt;&lt;span style="color:blue;"&gt;if &lt;/span&gt;(!settings.TryGetValue(NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionString,&lt;span style="color:blue;"&gt;out &lt;/span&gt;_connectionString))
    _connectionString = GetNamedConnectionString(settings);

        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(_connectionString == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:blue;"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;HibernateException&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Could not find connection string setting (set &amp;quot; 
                &lt;/span&gt;+ NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionString + &lt;span style="color:#a31515;"&gt;&amp;quot; or &amp;quot; 
                &lt;/span&gt;+ NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionStringName + &lt;span style="color:#a31515;"&gt;&amp;quot; property)&amp;quot;&lt;/span&gt;);
        }
        ConfigureDriver(settings);
    }&lt;/pre&gt;
&lt;p&gt;This is necessary because the original connection
string is private, but just copy the code from base method.&lt;/p&gt;
&lt;p&gt;The real magic is in &amp;quot;ConnectionString&amp;quot;
property, it is called when nh has to connect in a Session. You have to
override it so you can make the changes you need.&lt;span style="white-space:pre;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;protected override string &lt;/span&gt;ConnectionString
{
    &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;FixConnectionString(_connectionString); }
}
&lt;/pre&gt;
&lt;p&gt;In this case &lt;i&gt;FixConnectionString&lt;/i&gt;&amp;nbsp;read
the user info from some environment variable and inject it in the connection
string.&lt;/p&gt;
&lt;p&gt;Finally configure NH to use the
ConnectionProvider:&lt;/p&gt;
&lt;pre class="code"&gt;            &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;connection.provider&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;&amp;gt;
                &lt;/span&gt;MyAssembly.DynamicConnectionProvider, MyAssembly
            &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Some useful articles&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;&lt;a style="text-decoration:none;" href="http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html"&gt;here&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx"&gt;here&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Dynamically change user info in connection string</title><link>http://nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string/revision/4.aspx</link><pubDate>Sat, 29 Nov 2008 05:16:58 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:378</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 4 posted to How to by Fabio Maulo on 29/11/2008 02:16:58 a.m.&lt;br /&gt;
&lt;h2&gt;Dynamically change user info in connection string&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;Mapping&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;QuickStart&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;In some cases our clients has to use the same database
user id in each connection, so they can use audit and security features of
their database system (and their DBAs will be happy [:)]).&lt;/p&gt;
&lt;p&gt;To do that in Nh We can use the ConnectionProvider
facility. Just derive a class from the standard&amp;nbsp;DriverConnectionProvider
class:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DynamicConnectionProvider &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;DriverConnectionProvider
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private string &lt;/span&gt;_connectionString;
    &lt;span style="color:blue;"&gt;public override void &lt;/span&gt;Configure(&lt;span style="color:#2b91af;"&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt; settings)
    {

        &lt;span style="color:green;"&gt;// Connection string in the configuration overrides named connection string
        &lt;/span&gt;&lt;span style="color:blue;"&gt;if &lt;/span&gt;(!settings.TryGetValue(NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionString,&lt;span style="color:blue;"&gt;out &lt;/span&gt;_connectionString))
    _connectionString = GetNamedConnectionString(settings);

        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(_connectionString == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:blue;"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;HibernateException&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Could not find connection string setting (set &amp;quot; 
                &lt;/span&gt;+ NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionString + &lt;span style="color:#a31515;"&gt;&amp;quot; or &amp;quot; 
                &lt;/span&gt;+ NHibernate.Cfg.&lt;span style="color:#2b91af;"&gt;Environment&lt;/span&gt;.ConnectionStringName + &lt;span style="color:#a31515;"&gt;&amp;quot; property)&amp;quot;&lt;/span&gt;);
        }
        ConfigureDriver(settings);
    }&lt;/pre&gt;
&lt;p&gt;This is necessary because the original connection
string is private, but just copy the code from base method.&lt;/p&gt;
&lt;p&gt;The real magic is in &amp;quot;ConnectionString&amp;quot;
property, it is called when nh has to connect in a Session. You have to
override it so you can make the changes you need.&lt;span style="white-space:pre;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;protected override string &lt;/span&gt;ConnectionString
{
    &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;FixConnectionString(_connectionString); }
}
&lt;/pre&gt;
&lt;p&gt;In this case &lt;em&gt;FixConnectionString&lt;/em&gt;&amp;nbsp;read
the user info from some environment variable and inject it in the connection
string.&lt;/p&gt;
&lt;p&gt;Finally configure NH to use the
ConnectionProvider:&lt;/p&gt;
&lt;pre class="code"&gt;            &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;connection.provider&lt;/span&gt;&amp;quot;&lt;span style="color:blue;"&gt;&amp;gt;
                &lt;/span&gt;MyAssembly.DynamicConnectionProvider, MyAssembly
            &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Some useful articles&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;&lt;a style="text-decoration:none;" href="http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html"&gt;here&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;[howtonh:howtonh:/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx|here]]&lt;/span&gt;&amp;nbsp;&lt;a href="/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Dynamically change user info in connection string</title><link>http://nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string/revision/3.aspx</link><pubDate>Sat, 29 Nov 2008 05:14:55 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:203</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 3 posted to How to by Fabio Maulo on 29/11/2008 02:14:55 a.m.&lt;br /&gt;
&lt;h2&gt;Dynamically change user info in connection string&lt;/h2&gt;
&lt;p&gt;In some cases our clients has to use the same database
user id in each connection, so they can use audit and security features of
their database system (and their DBAs will be happy [:)]).&lt;/p&gt;
&lt;p&gt;To do that in Nh We can use the ConnectionProvider
facility. Just derive a class from the standard&amp;nbsp;DriverConnectionProvider
class:&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DynamicConnectionProvider &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;DriverConnectionProvider
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private string &lt;/span&gt;_connectionString;
    &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Override&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Configure&lt;/span&gt;&lt;/strong&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt;&lt;span style="color:blue;"&gt;public override void &lt;span style="text-decoration: line-through; color: red;"&gt;Configure(IDictionary&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;string,&lt;/span&gt; &lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Configure&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;&lt;span style="background: SpringGreen;"&gt;IDictionary&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt; settings)
    {

        &lt;span style="color:green;"&gt;// Connection string in the configuration overrides named connection string
        &lt;/span&gt;&lt;span style="color:blue;"&gt;if &lt;/span&gt;(&lt;span style="text-decoration: line-through; color: red;"&gt;!settings.TryGetValue(NHibernate.Cfg.Environment.ConnectionString,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;!settings.TryGetValue(NHibernate.Cfg.&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;&lt;span style="background: SpringGreen;"&gt;Environment&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ConnectionString,&lt;/span&gt;&lt;span style="color:blue;"&gt;out &lt;/span&gt;_connectionString))
    _connectionString = GetNamedConnectionString(settings);

        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(_connectionString == &lt;span style="color:blue;"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color:blue;"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;HibernateException&lt;/span&gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Could not find connection string setting (set &amp;quot; 
                &lt;/span&gt;+ &amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Cfg.Environment.ConnectionString&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.Cfg&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;&lt;span style="background: SpringGreen;"&gt;Environment&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ConnectionString&lt;/span&gt; + &lt;span style="color:#a31515;"&gt;&amp;quot; or &amp;quot; 
                &lt;/span&gt;+ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Cfg.Environment.ConnectionStringName&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.Cfg&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;&lt;span style="background: SpringGreen;"&gt;Environment&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ConnectionStringName&lt;/span&gt; + &lt;span style="color:#a31515;"&gt;&amp;quot; property)&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;);&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
        }
        ConfigureDriver(settings);
    }&lt;/pre&gt;
&lt;p&gt;This is necessary because the original connection
string is private, but just copy the code from base method.&lt;/p&gt;
&lt;p&gt;The real magic is in &amp;quot;ConnectionString&amp;quot;
property, it is called when nh has to connect in a Session. You have to
override it so you can make the changes you need.&lt;span style="white-space:pre;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;protected override string &lt;/span&gt;ConnectionString
{
    &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;FixConnectionString(_connectionString); }
}
&lt;/pre&gt;
&lt;p&gt;In this case &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;FixConnectionString&amp;quot;&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;FixConnectionString&lt;/span&gt;&lt;/em&gt;&amp;nbsp;read
the user info from some environment variable and inject it in the connection
string.&lt;/p&gt;
&lt;p&gt;Finally configure &lt;span style="text-decoration: line-through; color: red;"&gt;Nh&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NH&lt;/span&gt; to use the
ConnectionProvider:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.provider&amp;quot;&amp;gt;MyAssembly.DynamicConnectionProvider,&lt;/span&gt; &lt;/p&gt;
&lt;pre class="code"&gt;            &lt;span style="color:blue;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;/span&gt;&lt;span style="color:red;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="background: SpringGreen;"&gt;connection.provider&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;
                &lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;MyAssembly.DynamicConnectionProvider&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; MyAssembly
            &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Some useful articles&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html&lt;/span&gt;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;&lt;a href="http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html"&gt;&lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;/a&gt;&amp;nbsp;and&lt;/p&gt;
&lt;p&gt;&lt;a href="/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;http://nhforge.org/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;[howtonh:howtonh:/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx|here]]&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Dynamically change user info in connection string</title><link>http://nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string/revision/2.aspx</link><pubDate>Sat, 29 Nov 2008 00:41:57 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:202</guid><dc:creator>oorduz</dc:creator><description>Revision 2 posted to How to by oorduz on 28/11/2008 09:41:57 p.m.&lt;br /&gt;
&lt;h2&gt;Dynamically change user info in connection string&lt;/h2&gt;
&lt;p&gt;In some cases our clients has to use the same database
user id in each connection, so they can use audit and security features of
their database system (and their DBAs will be happy [:)]).&lt;/p&gt;
&lt;p&gt;To do that in Nh We can use the ConnectionProvider
facility. Just derive a class from the standard&amp;nbsp;DriverConnectionProvider
class:&lt;/p&gt;
&lt;p&gt;public class
DynamicConnectionProvider : DriverConnectionProvider&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private string
_connectionString;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Override the &lt;strong&gt;Configure&lt;/strong&gt; method:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void Configure(IDictionary&amp;lt;string, string&amp;gt;
settings)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Connection string in the configuration overrides named
connection string&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!settings.TryGetValue(NHibernate.Cfg.Environment.ConnectionString, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
out _connectionString))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_connectionString =
GetNamedConnectionString(settings);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (_connectionString == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new HibernateException(&amp;quot;Could not find
connection string setting (set &amp;quot; +&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NHibernate.Cfg.Environment.ConnectionString &amp;nbsp;+&amp;nbsp;&amp;quot; or &amp;quot; +&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NHibernate.Cfg.Environment.ConnectionStringName +&amp;nbsp;&amp;quot; property)&amp;quot;);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ConfigureDriver(settings);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; necessary because the original connection
string is private, but just copy the code from base method.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The real magic is in &amp;quot;ConnectionString&amp;quot;
property, it is called when nh has to connect in a Session. You have to
override it so you can make the changes you need.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;protected override string ConnectionString&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return FixConnectionString(_connectionString);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In this case &amp;quot;FixConnectionString&amp;quot; read
the user info from some environment variable and inject it in the connection
string.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Finally configure Nh to use the
ConnectionProvider:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.provider&amp;quot;&amp;gt;MyAssembly.PsDynamicConnectionProvider,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.provider&amp;quot;&amp;gt;MyAssembly.DynamicConnectionProvider,&lt;/span&gt; MyAssembly &amp;lt;/property&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Some useful articles:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html"&gt;http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;&lt;a href="/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx"&gt;http://nhforge.org/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Dynamically change user info in connection string</title><link>http://nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string/revision/1.aspx</link><pubDate>Sat, 29 Nov 2008 00:29:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:201</guid><dc:creator>oorduz</dc:creator><description>Revision 1 posted to How to by oorduz on 28/11/2008 09:29:02 p.m.&lt;br /&gt;
&lt;p&gt;In some cases our clients has to use the same database
user id in each connection, so they can use audit and security features of
their database system (and their DBAs will be happy [:)]).&lt;/p&gt;
&lt;p&gt;To do that in Nh We can use the ConnectionProvider
facility. Just derive a class from the standard&amp;nbsp;DriverConnectionProvider
class:&lt;/p&gt;
&lt;p&gt;public class
DynamicConnectionProvider : DriverConnectionProvider&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private string
_connectionString;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Override the &lt;strong&gt;Configure&lt;/strong&gt; method:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void Configure(IDictionary&amp;lt;string, string&amp;gt;
settings)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Connection string in the configuration overrides named
connection string&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!settings.TryGetValue(NHibernate.Cfg.Environment.ConnectionString, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
out _connectionString))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_connectionString =
GetNamedConnectionString(settings);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (_connectionString == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new HibernateException(&amp;quot;Could not find
connection string setting (set &amp;quot; +&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NHibernate.Cfg.Environment.ConnectionString &amp;nbsp;+&amp;nbsp;&amp;quot; or &amp;quot; +&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NHibernate.Cfg.Environment.ConnectionStringName +&amp;nbsp;&amp;quot; property)&amp;quot;);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ConfigureDriver(settings);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This necessary because the original connection
string is private, but just copy the code from base method.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The real magic is in &amp;quot;ConnectionString&amp;quot;
property, it is called when nh has to connect in a Session. You have to
override it so you can make the changes you need.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;protected override string ConnectionString&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return FixConnectionString(_connectionString);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In this case &amp;quot;FixConnectionString&amp;quot; read
the user info from some environment variable and inject it in the connection
string.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Finally configure Nh to use the
ConnectionProvider:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;connection.provider&amp;quot;&amp;gt;MyAssembly.PsDynamicConnectionProvider, MyAssembly &amp;lt;/property&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Some useful articles:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html"&gt;http://japikse.blogspot.com/2008/06/custom-connection-provider-with.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;&lt;a href="/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx"&gt;http://nhforge.org/blogs/nhibernate/archive/2008/09/18/extending-nhibernate-s-driverconnectionprovider.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Mapping the same class to a view and a table using entity-name</title><link>http://nhforge.org/wikis/howtonh/mapping-the-same-class-to-a-view-and-a-table-using-entity-name/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:04:52 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:49</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:04:52 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping the same class to a view and a table using entity-name&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Session&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Architecture&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/div&gt;

&lt;div&gt;
&lt;h2&gt;The Scenario&lt;/h2&gt;
&lt;p&gt;You have a single class which you must map to both a table and a database view (or perhaps another table). The view is returning records from the table itself, usually filtered, and the view may have more or less columns than the table. A more practical example is a multi tenant database, where a table has a tenant column, and the view filters the records to show only the ones associated with the current tenant. In this case, the backing table has a tenant column, but the view doesn&amp;#39;t. To make things even more interesting, the table is in a separate database schema. For more details on this multi tenant architectural pattern &lt;a href="http://msdn.microsoft.com/en-us/library/aa479086.aspx#mlttntda_tvf"&gt;see this article&lt;/a&gt;. Other possible uses for this mapping technique include dynamically mapping the same base class to multiple subclass tables, which is &lt;a href="http://www.mattfreeman.co.uk/2009/01/nhibernate-21-trunk-entity-name-some-inheritance-and-dynamic-component/"&gt;described here&lt;/a&gt;, &lt;/p&gt;
&lt;p&gt;You want to map this in NHibernate, in a way that will allow you to specify which &amp;#39;representation&amp;#39; of the entity you want to see, either the table containing records for all tenants, or the view containing records for the currently active tenant. Perhaps your system needs to access all the records for all tenants to perform auditing or cleanup. In all other cases, you only want to show the records filtered for the current tenant, via the mapped view. &lt;/p&gt;
&lt;p&gt;So, can this be done? Thankfully, yes, but only if you like living on the cutting edge! &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;In order to set this up with NHibernate, you will need to use the up and coming version 2.1, as we will be taking advantage of the new &amp;#39;entity-name&amp;#39; feature. This means you will need to checkout and build the latest version of NHibernate. There&amp;#39;s plenty of &lt;a href="/blogs/nhibernate/archive/2008/09/06/prepare-your-system-for-nhibernate.aspx"&gt;guidance online about this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this article, I&amp;#39;ll be working against version 2.1.0.1001, revision 4099. &lt;/p&gt;
&lt;p&gt;If you are using NHibernate.Linq, there&amp;#39;s some special considerations that I&amp;#39;ll explain later. You&amp;#39;ll need to modify this library to take advantage of entity names, so you will need to checkout and build it &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/"&gt;from the nhcontrib project:&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I&amp;#39;ll be working against version 1.0.0.0, revision 803. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Setting up the mappings&lt;/h2&gt;
&lt;p&gt;The class we&amp;#39;re using is fairly simple, as the magic happens in the mapping. Note that the TenantId property is nullable, as it wont be mapped when we are using the view, more on that later. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt; {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; Id { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; MyProperty { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;? TenantId { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The database is also pretty straightforward, we have a table which maps to the entity class, and a view which returns records from that table. Notice that the view has no tenant id. Also notice that both of these objects are in different schemas. This just highlights the fact that the mappings for the two objects would have to be different, even if the properties were identical. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;span&gt; [AllItemsSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]&lt;/span&gt;&lt;span style="color:#808080;"&gt;(
&lt;/span&gt;&lt;span&gt;
	[ID] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,
&lt;/span&gt;&lt;span&gt;
	[MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;200&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;NULL,
	&lt;/span&gt;&lt;span&gt;
	[TenantId] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,
	&lt;/span&gt;&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt;&lt;span&gt; [PK_MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;[ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;)
)
&lt;/span&gt;

&lt;span style="color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;VIEW&lt;/span&gt;&lt;span&gt; [TenantSpecificSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;
AS&lt;/span&gt;&lt;span&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;
SELECT&lt;/span&gt;&lt;span&gt; [ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt;&lt;span&gt; [MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span&gt; [AllItemsSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;span&gt; [TenantId] &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span&gt; dbo&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;GetCurrentTenant&lt;/span&gt;&lt;span style="color:#808080;"&gt;()
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;So far so good. Now we move on to the mappings. Firstly, the table specific mapping. This will allow us to access all of the records for a particular entity, regardless of the tenant they are associated with. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Table&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`&lt;span&gt;AllItemsSchema&lt;/span&gt;`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TenantId&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Some things to notice here are: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The &amp;#39;schema&amp;#39; attribute is set to &amp;#39;AllItemsSchema&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;entity-name&amp;#39; attribute is specified. In this case we are keeping it fairly simple, in the real world you would want to use something that&amp;#39;s unique for each entity.
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is mapped
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest of the mapping is the exactly the same as you would expect. As I noted above, the entity name here is deliberately simplistic. Its also worth mentioning that the entity name is used to uniquely identity an entity mapping across the entire session factory, so you cant repeat it on different class mappings. Next, on to the mapping of the view, which will provide only the records for the currently active tenant. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;View&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`&lt;span&gt;TenantSpecificSchema&lt;/span&gt;`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema-action&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;none&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;This is where things get interesting: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			We have used the same table and class name, but notice that the &amp;#39;entity-name&amp;#39; is different
		&lt;/li&gt;
&lt;li&gt;
			We have a different database schema
			&amp;#39;&lt;span&gt;TenantSpecificSchema&lt;/span&gt;&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The TenantId property is absent as the view does not provide this
		&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;schema-action&amp;#39; is set to none, this will prevent NHibernate from including this mapping in its schema export, it would otherwise attempt to create a table for this view
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Although we are using the same class in both cases, the mappings can be quite different. In this example I&amp;#39;ve included the TenantId property on the entity class even though it will always be null when the view mapping is used. Whether you want to do this yourself is a matter of taste. One possibility would be to implement the TenantId property with an explicit interface, which would hide it from the consumers of the class unless needed. &lt;/p&gt;
&lt;p&gt;Now that we have the mappings we can spin up a session factory using your favorite technique. Because we have used entity names for both of our mappings, you will see that we cant just store and retrieve entities by their type as usual. The entities are no longer identified by their types, but their entity name instead. Initially, I had planned to specify the entity name only for one of the mappings, and leave the other mapping as a &amp;#39;default&amp;#39; with just its [Type] &amp;#39;name&amp;#39; specified, but as I will explain later in more detail, this wont work as expected. If you want to reliably use entity name on one mapping of the class, you must use an entity name on them all. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Entity name and the ISession&lt;/h2&gt;
&lt;p&gt;In the latest revision of NHibernate, all the methods of ISession which take a System.Type have been overloaded to accept a string specifying the entity name. Apart form this, working with entity name mappings is exactly the same as their Type counterparts. If you are using a generic implementation of IRepository, things get even easier, as you can control the entity name in a single place, for example: &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;enum&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; {
	Default,
	Table,
	View
}

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; : &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; NHibernateRepository(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode) {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span&gt;.session = session;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	this&lt;/span&gt;&lt;span&gt;.mode = mode;
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Get(TKey key) {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; (TEntity)session.Get(GetEntityName(), key);
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; List() {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; session.CreateCriteria(GetEntityName()).List&amp;lt;TEntity&amp;gt;();
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span&gt; SaveOrUpdate(TEntity entity) {
		session.SaveOrUpdate(GetEntityName(), entity);
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; GetEntityName() {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;switch&lt;/span&gt;&lt;span&gt; (mode) {
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;Table&amp;quot;&lt;/span&gt;&lt;span&gt;;
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;View&amp;quot;&lt;/span&gt;&lt;span&gt;;
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;default&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span&gt;(TEntity).FullName;
		}
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode;
}
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;The entity name to use is based on the parameter we pass into the repository. In the real world you will want to use something more sophisticated to control the entity names. By specifying the Table mode when creating this repository, all the operations we perform against it will now deal with the records for all tenants. If we only want to deal with the records for a specific tenant, then we pass in the View mode. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for all tenants&lt;/span&gt;
	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; allTenantsRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table);

	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for the current tenant only&lt;/span&gt;
	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; currentTenantRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View);
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;One thing that&amp;#39;s important to realize is that internally, NHibernate implicitly creates an entity name whether you specify one explicitly or not. The entity name it uses by default is the FullName of the mapped type. This means that passing in the Type.FullName to the methods accepting a string has the same effect as passing in the System.Type itself. This can make it easier to manage your Repository implementation as you can use the string overloads exclusively, and pass in the type name when no specific entity name is required. &lt;/p&gt;
&lt;p&gt;Now we have a repository that we can use to work with the two different profiles for our entities, as well as falling back to the standard behavior when no entity name mappings are required. This approach can be extended in any number of ways to support various complex mapping scenarios. &lt;/p&gt;
&lt;p&gt;Its worth mentioning that you can also specify the entity name via IInterceptor.GetEntityName(object), but I haven&amp;#39;t done that here because it only kicks in when saving or updating transient instances, as far as I could tell. You still have to supply the entity name to the ISession methods when querying the database, and I prefer to keep the entity name behavior in one place. &lt;/p&gt;
&lt;p&gt;If you want to use NHibernate.Linq with this approach then you will soon find that there&amp;#39;s currently (r.803) no way to specify the entity name when using the Linq&amp;lt;T&amp;gt; extension method on ISession. Not to fear, there&amp;#39;s a fairly straightforward way to provide this feature with minimal impact on any querying code you may have. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Using Entity Name with Linq to NHibernate&lt;/h2&gt;
&lt;p&gt;Lets start by adding a protected property to our Repository implementation which will provide us with an INHibernateQueryable&amp;lt;T&amp;gt;. All we need to know about that type for now is that its returned from the ISession.Linq&amp;lt;T&amp;gt; extension method. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; session.Linq&amp;lt;TEntity&amp;gt;();
		}
	}
&lt;/span&gt;
	&lt;span style="color:#008000;"&gt;//the rest of the repository is exactly the same...&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;We can now use Linq functionality in our repository by starting with that property. Lets add a useful Find() method to our repository that accepts a predicate which we can pass to the Linq implementation. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Find(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Func&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt; predicate) {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; Linq.Where(predicate).FirstOrDefault();
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;There&amp;#39;s one problem with this approach however. As you can see in the Linq property, we are passing in the current generic type of our repository. This means that our entity name mappings are not being used, and even worse than that, NHibernate will throw an error informing us that the type in question could not be found. This is to be expected, recall that we are overriding the implicit entity name, which defaults to the mapped types name, with our own. &lt;/p&gt;
&lt;p&gt;The current implementation of NHibernate.Linq is based on ICriteria, and ISession.CreateCriteria has an overload that will accept an entity name, just like all the other ISession methods. Because the entity name (or the Type) of an ICriteria can not be modified after it is created, we must control the creation of the ICriteria ourselves. The way I chose to do it was by adding a function callback to the QueryOptions object in NHibernate.Linq. This callback accepts the Linq ISession and the current expression tree and returns a new ICriteria. This lets us quickly create a delegate that will call Session.CreateCriteria(string) &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {
			&lt;/span&gt;&lt;span style="color:#008000;"&gt;//create the new ICriteria with the current entity name, as opposed to the entity type&lt;/span&gt;
			&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; queryable = Session.Linq&amp;lt;TEntity&amp;gt;();
			queryable.QueryOptions.CreateCriteraFunction = (s, e) =&amp;gt; s.CreateCriteria(GetEntityName());&lt;/span&gt;
			
			&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; queryable;
		}
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;Because we are already using this property in our Find() method above, no changes are necessary, it will pick up this functionality automatically. &lt;/p&gt;
&lt;p&gt;The changes to the NHibernate.Linq assembly are quite modest, I have included a diff in this article which you can apply to r.803 to enable this feature. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Some Gotchas&lt;/h2&gt;
&lt;p&gt;As I mentioned previously, NHibernate specifies an entity name for all entities, whether you specify one or not. If you don&amp;#39;t, the entity name is taken from the full name of the mapped type. This means that NHibernate (currently) has no way of telling whether the entity name was explicitly specified, or taken from the class. This has an interesting side effect, which I alluded to at the start of the article, and answers the question of why you cant use a &amp;#39;default&amp;#39; mapping with no explicit entity name. &lt;/p&gt;
&lt;p&gt;The internals of NHibernate still have some assumptions that an entity name will be a full type name, including namespaces. The way things are currently implemented, it will examine the entity name to see if it contains a period (&amp;#39;.&amp;#39;), and if it does, it is assumed to be a class name, if it does not, it is assumed to be a user specified entity name. This means that all the existing code that expects an entity to be identified by its type will continue to work, and therefore, this behaviour is not likely to change any time soon. &lt;/p&gt;
&lt;p&gt;So why is that important? Well, if you provide an entity name that contains a period, some sections of NHibernate (specifically &lt;a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SessionFactoryImpl.cs"&gt;SessionFactoryImpl.GetImplementors(string)&lt;/a&gt;) will assume it to be a type name and will start looking for all the mappings related to the persistent class. The upshot of this is that it will eventually find all of the mappings, regardless of their entity names that we defined, and use them both. &lt;/p&gt;
&lt;p&gt;I first noticed this when I was getting an unexpected number of results in my tests, sometimes twice as many as I was expecting. The NHibernate logs showed that there were two queries being issued, one for each of the mappings, (table and view). It turned out to be the entity names I was using: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The view specific mapping used the default (that us, unspecified) entity name: &amp;quot;My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			The table specific mapping used a mangled type name: &amp;quot;[AllTenants] My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, both of these entity names contain periods. This caused NHibernate to use both sets of mappings when performing any operation, regardless of the entity name I specified, hence the two queries being issued. The solution is to specify an explicit entity name for both mappings which contains no periods: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			View specific: &amp;quot;My_Namspace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			Table specific: &amp;quot;[AllTenants] My_Namespace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One downside of this approach, is that you must use the entity name in all references to the persistent class, i.e. in many-to-one and one-to-many elements. If you don&amp;#39;t, you will get an error when you try to create the session factory complaining about a reference to an unmapped type. Remember that we are overriding the default entity name behavior with our own, so NHibernate can no longer identify an entity using its type. In my particular case, I am generating the mappings dynamically, so this extra check is not a big problem, but if you are mapping by hand or using Fluent NHibernate, its something to be aware of. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I&amp;#39;ve talked about mapping a single class to multiple tables (or views) using the new entity name feature in the upcoming version of NHibernate. I&amp;#39;ve also provided some suggestions on working with entity specific mappings, as well as some of the things you need to be aware of when using them. Finally, I&amp;#39;ve shown how Linq to NHibernate can be adapted for use with entity names.&lt;a href="http://www.mustuniversity.com/programs/degree/doctorate-degree.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Doctorate&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Programs&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/certificate/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Certificate&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The example given here, of tenant specific filtering, is perhaps not the best way to get the job done. Something more along the lines of &lt;a href="/wikis/howtonh/contextual-data-using-nhibernate-filters.aspx"&gt;this article&lt;/a&gt; may be more appropriate for a greenfield application. But if you find yourself dealing with a legacy database that exhibits this requirement, the new features in NHibernate 2.1 will allow you to do so with little or no impact on your domain architecture.&lt;a href="http://www.mustuniversity.com/programs/diploma/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Programs&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Patch for NHibernate.Linq r.803&lt;/h2&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs	(revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs	(working copy)
@@ -39,7 +39,12 @@
 		{
 			if (rootCriteria == null)
 			{
-				rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+				if (options.CreateCriteraFunction != null) {
+					rootCriteria = options.CreateCriteraFunction(session, expr);
+				}
+				else {
+					rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+				}
 				options.Execute(rootCriteria);
 			}
 			return expr;
Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs	(revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs	(working copy)
@@ -1,4 +1,6 @@
 using System;
+using NHibernate.Criterion;
+using NHibernate.Linq.Expressions;
 
 namespace NHibernate.Linq
 {
@@ -9,6 +11,8 @@
 	{
 		private Action&amp;lt;ICriteria&amp;gt; action;
 
+		public Func&amp;lt;ISession, QuerySourceExpression, ICriteria&amp;gt; CreateCriteraFunction;
+
 		public QueryOptions()
 		{
 			this.action = delegate { };
	&lt;/pre&gt;
&lt;/div&gt;</description></item><item><title>Mapping the same class to a view and a table using entity-name</title><link>http://nhforge.org/wikis/howtonh/mapping-the-same-class-to-a-view-and-a-table-using-entity-name/revision/5.aspx</link><pubDate>Wed, 30 Mar 2011 11:09:39 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:513</guid><dc:creator>markweee</dc:creator><description>Revision 5 posted to How to by markweee on 30/03/2011 08:09:39 a.m.&lt;br /&gt;
&lt;h2&gt;Mapping the same class to a view and a table using entity-name&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture, query, entity name&lt;/div&gt;

&lt;div&gt;
&lt;h2&gt;The Scenario&lt;/h2&gt;
&lt;p&gt;You have a single class which you must map to both a table and a database view (or perhaps another table). The view is returning records from the table itself, usually filtered, and the view may have more or less columns than the table. A more practical example is a multi tenant database, where a table has a tenant column, and the view filters the records to show only the ones associated with the current tenant. In this case, the backing table has a tenant column, but the view doesn&amp;#39;t. To make things even more interesting, the table is in a separate database schema. For more details on this multi tenant architectural pattern &lt;a href="http://msdn.microsoft.com/en-us/library/aa479086.aspx#mlttntda_tvf"&gt;see this article&lt;/a&gt;. Other possible uses for this mapping technique include dynamically mapping the same base class to multiple subclass tables, which is &lt;a href="http://www.mattfreeman.co.uk/2009/01/nhibernate-21-trunk-entity-name-some-inheritance-and-dynamic-component/"&gt;described here&lt;/a&gt;, &lt;/p&gt;
&lt;p&gt;You want to map this in NHibernate, in a way that will allow you to specify which &amp;#39;representation&amp;#39; of the entity you want to see, either the table containing records for all tenants, or the view containing records for the currently active tenant. Perhaps your system needs to access all the records for all tenants to perform auditing or cleanup. In all other cases, you only want to show the records filtered for the current tenant, via the mapped view. &lt;/p&gt;
&lt;p&gt;So, can this be done? Thankfully, yes, but only if you like living on the cutting edge! &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;In order to set this up with NHibernate, you will need to use the up and coming version 2.1, as we will be taking advantage of the new &amp;#39;entity-name&amp;#39; feature. This means you will need to checkout and build the latest version of NHibernate. There&amp;#39;s plenty of &lt;a href="/blogs/nhibernate/archive/2008/09/06/prepare-your-system-for-nhibernate.aspx"&gt;guidance online about this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this article, I&amp;#39;ll be working against version 2.1.0.1001, revision 4099. &lt;/p&gt;
&lt;p&gt;If you are using NHibernate.Linq, there&amp;#39;s some special considerations that I&amp;#39;ll explain later. You&amp;#39;ll need to modify this library to take advantage of entity names, so you will need to checkout and build it &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/"&gt;from the nhcontrib project:&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I&amp;#39;ll be working against version 1.0.0.0, revision 803. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Setting up the mappings&lt;/h2&gt;
&lt;p&gt;The class we&amp;#39;re using is fairly simple, as the magic happens in the mapping. Note that the TenantId property is nullable, as it wont be mapped when we are using the view, more on that later. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt; {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; Id { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; MyProperty { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;? TenantId { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The database is also pretty straightforward, we have a table which maps to the entity class, and a view which returns records from that table. Notice that the view has no tenant id. Also notice that both of these objects are in different schemas. This just highlights the fact that the mappings for the two objects would have to be different, even if the properties were identical. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;span&gt; [AllItemsSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]&lt;/span&gt;&lt;span style="color:#808080;"&gt;(
&lt;/span&gt;&lt;span&gt;
	[ID] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,
&lt;/span&gt;&lt;span&gt;
	[MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;200&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;NULL,
	&lt;/span&gt;&lt;span&gt;
	[TenantId] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,
	&lt;/span&gt;&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt;&lt;span&gt; [PK_MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;[ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;)
)
&lt;/span&gt;

&lt;span style="color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;VIEW&lt;/span&gt;&lt;span&gt; [TenantSpecificSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;
AS&lt;/span&gt;&lt;span&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;
SELECT&lt;/span&gt;&lt;span&gt; [ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt;&lt;span&gt; [MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span&gt; [AllItemsSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;span&gt; [TenantId] &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span&gt; dbo&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;GetCurrentTenant&lt;/span&gt;&lt;span style="color:#808080;"&gt;()
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;So far so good. Now we move on to the mappings. Firstly, the table specific mapping. This will allow us to access all of the records for a particular entity, regardless of the tenant they are associated with. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Table&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`&lt;span&gt;AllItemsSchema&lt;/span&gt;`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TenantId&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Some things to notice here are: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The &amp;#39;schema&amp;#39; attribute is set to &amp;#39;AllItemsSchema&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;entity-name&amp;#39; attribute is specified. In this case we are keeping it fairly simple, in the real world you would want to use something that&amp;#39;s unique for each entity.
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is mapped
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest of the mapping is the exactly the same as you would expect. As I noted above, the entity name here is deliberately simplistic. Its also worth mentioning that the entity name is used to uniquely identity an entity mapping across the entire session factory, so you cant repeat it on different class mappings. Next, on to the mapping of the view, which will provide only the records for the currently active tenant. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;View&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`&lt;span&gt;TenantSpecificSchema&lt;/span&gt;`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema-action&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;none&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;This is where things get interesting: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			We have used the same table and class name, but notice that the &amp;#39;entity-name&amp;#39; is different
		&lt;/li&gt;
&lt;li&gt;
			We have a different database schema
			&amp;#39;&lt;span&gt;TenantSpecificSchema&lt;/span&gt;&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The TenantId property is absent as the view does not provide this
		&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;schema-action&amp;#39; is set to none, this will prevent NHibernate from including this mapping in its schema export, it would otherwise attempt to create a table for this view
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Although we are using the same class in both cases, the mappings can be quite different. In this example I&amp;#39;ve included the TenantId property on the entity class even though it will always be null when the view mapping is used. Whether you want to do this yourself is a matter of taste. One possibility would be to implement the TenantId property with an explicit interface, which would hide it from the consumers of the class unless needed. &lt;/p&gt;
&lt;p&gt;Now that we have the mappings we can spin up a session factory using your favorite technique. Because we have used entity names for both of our mappings, you will see that we cant just store and retrieve entities by their type as usual. The entities are no longer identified by their types, but their entity name instead. Initially, I had planned to specify the entity name only for one of the mappings, and leave the other mapping as a &amp;#39;default&amp;#39; with just its [Type] &amp;#39;name&amp;#39; specified, but as I will explain later in more detail, this wont work as expected. If you want to reliably use entity name on one mapping of the class, you must use an entity name on them all. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Entity name and the ISession&lt;/h2&gt;
&lt;p&gt;In the latest revision of NHibernate, all the methods of ISession which take a System.Type have been overloaded to accept a string specifying the entity name. Apart form this, working with entity name mappings is exactly the same as their Type counterparts. If you are using a generic implementation of IRepository, things get even easier, as you can control the entity name in a single place, for example: &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;enum&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; {
	Default,
	Table,
	View
}

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; : &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; NHibernateRepository(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode) {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span&gt;.session = session;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	this&lt;/span&gt;&lt;span&gt;.mode = mode;
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Get(TKey key) {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; (TEntity)session.Get(GetEntityName(), key);
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; List() {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; session.CreateCriteria(GetEntityName()).List&amp;lt;TEntity&amp;gt;();
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span&gt; SaveOrUpdate(TEntity entity) {
		session.SaveOrUpdate(GetEntityName(), entity);
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; GetEntityName() {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;switch&lt;/span&gt;&lt;span&gt; (mode) {
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;Table&amp;quot;&lt;/span&gt;&lt;span&gt;;
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;View&amp;quot;&lt;/span&gt;&lt;span&gt;;
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;default&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span&gt;(TEntity).FullName;
		}
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode;
}
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;The entity name to use is based on the parameter we pass into the repository. In the real world you will want to use something more sophisticated to control the entity names. By specifying the Table mode when creating this repository, all the operations we perform against it will now deal with the records for all tenants. If we only want to deal with the records for a specific tenant, then we pass in the View mode. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for all tenants&lt;/span&gt;
	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; allTenantsRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table);

	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for the current tenant only&lt;/span&gt;
	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; currentTenantRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View);
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;One thing that&amp;#39;s important to realize is that internally, NHibernate implicitly creates an entity name whether you specify one explicitly or not. The entity name it uses by default is the FullName of the mapped type. This means that passing in the Type.FullName to the methods accepting a string has the same effect as passing in the System.Type itself. This can make it easier to manage your Repository implementation as you can use the string overloads exclusively, and pass in the type name when no specific entity name is required. &lt;/p&gt;
&lt;p&gt;Now we have a repository that we can use to work with the two different profiles for our entities, as well as falling back to the standard behavior when no entity name mappings are required. This approach can be extended in any number of ways to support various complex mapping scenarios. &lt;/p&gt;
&lt;p&gt;Its worth mentioning that you can also specify the entity name via IInterceptor.GetEntityName(object), but I haven&amp;#39;t done that here because it only kicks in when saving or updating transient instances, as far as I could tell. You still have to supply the entity name to the ISession methods when querying the database, and I prefer to keep the entity name behavior in one place. &lt;/p&gt;
&lt;p&gt;If you want to use NHibernate.Linq with this approach then you will soon find that there&amp;#39;s currently (r.803) no way to specify the entity name when using the Linq&amp;lt;T&amp;gt; extension method on ISession. Not to fear, there&amp;#39;s a fairly straightforward way to provide this feature with minimal impact on any querying code you may have. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Using Entity Name with Linq to NHibernate&lt;/h2&gt;
&lt;p&gt;Lets start by adding a protected property to our Repository implementation which will provide us with an INHibernateQueryable&amp;lt;T&amp;gt;. All we need to know about that type for now is that its returned from the ISession.Linq&amp;lt;T&amp;gt; extension method. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; session.Linq&amp;lt;TEntity&amp;gt;();
		}
	}
&lt;/span&gt;
	&lt;span style="color:#008000;"&gt;//the rest of the repository is exactly the same...&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;We can now use Linq functionality in our repository by starting with that property. Lets add a useful Find() method to our repository that accepts a predicate which we can pass to the Linq implementation. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Find(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Func&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt; predicate) {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; Linq.Where(predicate).FirstOrDefault();
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;There&amp;#39;s one problem with this approach however. As you can see in the Linq property, we are passing in the current generic type of our repository. This means that our entity name mappings are not being used, and even worse than that, NHibernate will throw an error informing us that the type in question could not be found. This is to be expected, recall that we are overriding the implicit entity name, which defaults to the mapped types name, with our own. &lt;/p&gt;
&lt;p&gt;The current implementation of NHibernate.Linq is based on ICriteria, and ISession.CreateCriteria has an overload that will accept an entity name, just like all the other ISession methods. Because the entity name (or the Type) of an ICriteria can not be modified after it is created, we must control the creation of the ICriteria ourselves. The way I chose to do it was by adding a function callback to the QueryOptions object in NHibernate.Linq. This callback accepts the Linq ISession and the current expression tree and returns a new ICriteria. This lets us quickly create a delegate that will call Session.CreateCriteria(string) &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {
			&lt;/span&gt;&lt;span style="color:#008000;"&gt;//create the new ICriteria with the current entity name, as opposed to the entity type&lt;/span&gt;
			&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; queryable = Session.Linq&amp;lt;TEntity&amp;gt;();
			queryable.QueryOptions.CreateCriteraFunction = (s, e) =&amp;gt; s.CreateCriteria(GetEntityName());&lt;/span&gt;
			
			&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; queryable;
		}
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;Because we are already using this property in our Find() method above, no changes are necessary, it will pick up this functionality automatically. &lt;/p&gt;
&lt;p&gt;The changes to the NHibernate.Linq assembly are quite modest, I have included a diff in this article which you can apply to r.803 to enable this feature. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Some Gotchas&lt;/h2&gt;
&lt;p&gt;As I mentioned previously, NHibernate specifies an entity name for all entities, whether you specify one or not. If you don&amp;#39;t, the entity name is taken from the full name of the mapped type. This means that NHibernate (currently) has no way of telling whether the entity name was explicitly specified, or taken from the class. This has an interesting side effect, which I alluded to at the start of the article, and answers the question of why you cant use a &amp;#39;default&amp;#39; mapping with no explicit entity name. &lt;/p&gt;
&lt;p&gt;The internals of NHibernate still have some assumptions that an entity name will be a full type name, including namespaces. The way things are currently implemented, it will examine the entity name to see if it contains a period (&amp;#39;.&amp;#39;), and if it does, it is assumed to be a class name, if it does not, it is assumed to be a user specified entity name. This means that all the existing code that expects an entity to be identified by its type will continue to work, and therefore, this behaviour is not likely to change any time soon. &lt;/p&gt;
&lt;p&gt;So why is that important? Well, if you provide an entity name that contains a period, some sections of NHibernate (specifically &lt;a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SessionFactoryImpl.cs"&gt;SessionFactoryImpl.GetImplementors(string)&lt;/a&gt;) will assume it to be a type name and will start looking for all the mappings related to the persistent class. The upshot of this is that it will eventually find all of the mappings, regardless of their entity names that we defined, and use them both. &lt;/p&gt;
&lt;p&gt;I first noticed this when I was getting an unexpected number of results in my tests, sometimes twice as many as I was expecting. The NHibernate logs showed that there were two queries being issued, one for each of the mappings, (table and view). It turned out to be the entity names I was using: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The view specific mapping used the default (that us, unspecified) entity name: &amp;quot;My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			The table specific mapping used a mangled type name: &amp;quot;[AllTenants] My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, both of these entity names contain periods. This caused NHibernate to use both sets of mappings when performing any operation, regardless of the entity name I specified, hence the two queries being issued. The solution is to specify an explicit entity name for both mappings which contains no periods: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			View specific: &amp;quot;My_Namspace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			Table specific: &amp;quot;[AllTenants] My_Namespace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One downside of this approach, is that you must use the entity name in all references to the persistent class, i.e. in many-to-one and one-to-many elements. If you don&amp;#39;t, you will get an error when you try to create the session factory complaining about a reference to an unmapped type. Remember that we are overriding the default entity name behavior with our own, so NHibernate can no longer identify an entity using its type. In my particular case, I am generating the mappings dynamically, so this extra check is not a big problem, but if you are mapping by hand or using Fluent NHibernate, its something to be aware of. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I&amp;#39;ve talked about mapping a single class to multiple tables (or views) using the new entity name feature in the upcoming version of NHibernate. I&amp;#39;ve also provided some suggestions on working with entity specific mappings, as well as some of the things you need to be aware of when using them. Finally, I&amp;#39;ve shown how Linq to NHibernate can be adapted for use with entity names. &lt;a href="http://www.mustuniversity.com/programs/degree/doctorate-degree.html"&gt;&lt;span style="background: SpringGreen;"&gt;Doctorate&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/"&gt;&lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Programs&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/certificate/"&gt;&lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Certificate&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The example given here, of tenant specific filtering, is perhaps not the best way to get the job done. Something more along the lines of &lt;a href="/wikis/howtonh/contextual-data-using-nhibernate-filters.aspx"&gt;this article&lt;/a&gt; may be more appropriate for a greenfield application. But if you find yourself dealing with a legacy database that exhibits this requirement, the new features in NHibernate 2.1 will allow you to do so with little or no impact on your domain architecture. &lt;a href="http://www.mustuniversity.com/programs/diploma/"&gt;&lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/"&gt;&lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Programs&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Patch for NHibernate.Linq r.803&lt;/h2&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs	(revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs	(working copy)
@@ -39,7 +39,12 @@
 		{
 			if (rootCriteria == null)
 			{
-				rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+				if (options.CreateCriteraFunction != null) {
+					rootCriteria = options.CreateCriteraFunction(session, expr);
+				}
+				else {
+					rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+				}
 				options.Execute(rootCriteria);
 			}
 			return expr;
Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs	(revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs	(working copy)
@@ -1,4 +1,6 @@
 using System;
+using NHibernate.Criterion;
+using NHibernate.Linq.Expressions;
 
 namespace NHibernate.Linq
 {
@@ -9,6 +11,8 @@
 	{
 		private Action&amp;lt;ICriteria&amp;gt; action;
 
+		public Func&amp;lt;ISession, QuerySourceExpression, ICriteria&amp;gt; CreateCriteraFunction;
+
 		public QueryOptions()
 		{
 			this.action = delegate { };
	&lt;/pre&gt;
&lt;/div&gt;</description></item><item><title>Mapping the same class to a view and a table using entity-name</title><link>http://nhforge.org/wikis/howtonh/mapping-the-same-class-to-a-view-and-a-table-using-entity-name/revision/4.aspx</link><pubDate>Fri, 27 Feb 2009 20:37:38 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:370</guid><dc:creator>Robert Byrne</dc:creator><description>Revision 4 posted to How to by Robert Byrne on 27/02/2009 05:37:38 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping the same class to a view and a table using entity-name&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;Session&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Architecture&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/div&gt;

&lt;div&gt;
&lt;h2&gt;The Scenario&lt;/h2&gt;
&lt;p&gt;You have a single class which you must map to both a table and a database view (or perhaps another table). The view is returning records from the table itself, usually filtered, and the view may have more or less columns than the table. A more practical example is a multi tenant database, where a table has a tenant column, and the view filters the records to show only the ones associated with the current tenant. In this case, the backing table has a tenant column, but the view doesn&amp;#39;t. To make things even more interesting, the table is in a separate database schema. &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;more&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;details&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;multi&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tenant&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;architectural&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;pattern&lt;/span&gt; &lt;a href="http://msdn.microsoft.com/en-us/library/aa479086.aspx#mlttntda_tvf"&gt;&lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;article&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; Other possible uses &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;technique&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;include&lt;/span&gt; dynamically mapping the same base class to multiple subclass tables, which is &lt;a href="http://www.mattfreeman.co.uk/2009/01/nhibernate-21-trunk-entity-name-some-inheritance-and-dynamic-component/"&gt;described here&lt;/a&gt;, &lt;/p&gt;
&lt;p&gt;You want to map this in NHibernate, in a way that will allow you to specify which &amp;#39;representation&amp;#39; of the entity you want to see, either the table containing records for all tenants, or the view containing records for the currently active tenant. &lt;span style="text-decoration: line-through; color: red;"&gt;Why&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;?&lt;/span&gt; Perhaps your system needs to access all the records for all tenants to perform auditing or cleanup&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;things&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;like&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt;. In all other cases, you only want to show the records &lt;span style="background: SpringGreen;"&gt;filtered&lt;/span&gt; for the current tenant, via the mapped view. &lt;/p&gt;
&lt;p&gt;So, can this be done? Thankfully, yes, but only if you like living on the cutting edge! &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;In order to set this up with NHibernate, you will need to use the up and coming version 2.1, as we will be taking advantage of the new &amp;#39;entity-name&amp;#39; feature. This means you will need to checkout and build the latest version of NHibernate. There&amp;#39;s plenty of &lt;a href="/blogs/nhibernate/archive/2008/09/06/prepare-your-system-for-nhibernate.aspx"&gt;guidance online about this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this article, I&amp;#39;ll be working against version 2.1.0.1001, revision 4099. &lt;/p&gt;
&lt;p&gt;If you are using NHibernate.Linq, there&amp;#39;s some special considerations that I&amp;#39;ll explain later. You&amp;#39;ll need to modify this library to take advantage of entity names, so you will need to checkout and build it &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/"&gt;from the nhcontrib project:&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I&amp;#39;ll be working against version 1.0.0.0, revision 803. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Setting up the mappings&lt;/h2&gt;
&lt;p&gt;The class we&amp;#39;re using is fairly simple, as the magic happens in the mapping. Note that the TenantId property is nullable, as it wont be mapped when we are using the view, more on that later. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt; {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; Id { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; MyProperty { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;? TenantId { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The database is also pretty straightforward, we have a table which maps to the entity class, and a view which returns records from that table. Notice that the view has no tenant id. Also notice that both of these objects are in different schemas. This just highlights the fact that the mappings for the two objects would have to be different, even if the properties were identical. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;span&gt; [&lt;span style="text-decoration: line-through; color: red;"&gt;TableSchema]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AllItemsSchema]&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]&lt;/span&gt;&lt;span style="color:#808080;"&gt;(
&lt;/span&gt;&lt;span&gt;
	[ID] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,
&lt;/span&gt;&lt;span&gt;
	[MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;200&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;NULL,
	&lt;/span&gt;&lt;span&gt;
	[TenantId] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,
	&lt;/span&gt;&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt;&lt;span&gt; [PK_MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;[ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;)
)
&lt;/span&gt;

&lt;span style="color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;VIEW&lt;/span&gt;&lt;span&gt; [&lt;span style="text-decoration: line-through; color: red;"&gt;dbo]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;TenantSpecificSchema]&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;
AS&lt;/span&gt;&lt;span&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;
SELECT&lt;/span&gt;&lt;span&gt; [ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt;&lt;span&gt; [MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span&gt; [&lt;span style="text-decoration: line-through; color: red;"&gt;TableSchema]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AllItemsSchema]&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;span&gt; [TenantId] &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span&gt; dbo&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;GetCurrentTenant&lt;/span&gt;&lt;span style="color:#808080;"&gt;()
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;So far so good. Now we move on to the mappings. Firstly, the table specific mapping. This will allow us to access all of the records for a particular entity, regardless of the tenant they are associated with. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Table&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`&lt;span style="text-decoration: line-through; color: red;"&gt;TableSchema`&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;AllItemsSchema&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TenantId&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Some things to notice here are: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The &amp;#39;schema&amp;#39; attribute is set to &amp;#39;&lt;span style="text-decoration: line-through; color: red;"&gt;TableSchema&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AllItemsSchema&lt;/span&gt;&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;entity-name&amp;#39; attribute is specified. In this case we are keeping it fairly simple, in the real world you would want to use something that&amp;#39;s unique for each entity.
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is mapped
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest of the mapping is the exactly the same as you would expect. As I noted above, the entity name here is deliberately simplistic. Its also worth mentioning that the entity name is used to uniquely identity an entity mapping across the entire session factory, so you cant repeat it on different class mappings. Next, on to the mapping of the view, which will provide only the records for the currently active tenant. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;View&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`&lt;span style="text-decoration: line-through; color: red;"&gt;dbo`&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;TenantSpecificSchema&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema-action&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;none&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;This is where things get interesting: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			We have used the same table and class name, but notice that the &amp;#39;entity-name&amp;#39; is different
		&lt;/li&gt;
&lt;li&gt;
			We have a different database schema
			&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;TenantSpecificSchema&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;
			The TenantId property is absent as the view does not provide this
		&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;schema-action&amp;#39; is set to none, this will prevent NHibernate from including this mapping in its schema export, it would otherwise attempt to create a table for this view
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Although we are using the same class in both cases, the mappings can be quite different. In this example I&amp;#39;ve included the TenantId property on the entity class even though it will always be null when the view mapping is used. Whether you want to do this yourself is a matter of taste. One possibility would be to implement the TenantId property with an explicit interface, which would hide it from the consumers of the class unless needed. &lt;/p&gt;
&lt;p&gt;Now that we have the mappings we can spin up a session factory using your &lt;span style="text-decoration: line-through; color: red;"&gt;favourite&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;favorite&lt;/span&gt; technique. Because we have used entity names for both of our mappings, you will see that we cant just store and retrieve entities by their type as usual. The entities are no longer identified by their types, but their entity name instead. Initially, I had planned to specify the entity name only for one of the mappings, and leave the other mapping as a &amp;#39;default&amp;#39; with just its [Type] &amp;#39;name&amp;#39; specified, but as I will explain later in more detail, this wont work as expected. If you want to reliably use entity name on one mapping of the class, you must use an entity name on them all. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Entity name and the ISession&lt;/h2&gt;
&lt;p&gt;In the latest revision of NHibernate, all the methods of ISession which take a System.Type have been overloaded to accept a string specifying the entity name. Apart form this, working with entity name mappings is exactly the same as their Type counterparts. If you are using a generic implementation of IRepository, things get even easier, as you can control the entity name in a single place, for example: &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;enum&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; {
	Default,
	Table,
	View
}

&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; : &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; NHibernateRepository(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode) {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span&gt;.session = session;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	this&lt;/span&gt;&lt;span&gt;.mode = mode;
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Get(TKey key) {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; (TEntity)session.Get(GetEntityName(), key);
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; List() {
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; session.CreateCriteria(GetEntityName()).List&amp;lt;TEntity&amp;gt;();
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span&gt; SaveOrUpdate(TEntity entity) {
		session.SaveOrUpdate(GetEntityName(), entity);
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; GetEntityName() {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;switch&lt;/span&gt;&lt;span&gt; (mode) {
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;Table&amp;quot;&lt;/span&gt;&lt;span&gt;;
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;View&amp;quot;&lt;/span&gt;&lt;span&gt;;
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;default&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span&gt;(TEntity).FullName;
		}
	}
	
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode;
}
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;The entity name to use is based on the parameter we pass into the repository. In the real world you will want to use something more sophisticated to control the entity names. By specifying the Table mode when creating this repository, all the operations we perform against it will now deal with the records for all tenants. If we only want to deal with the records for a specific tenant, then we pass in the View mode. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for all tenants&lt;/span&gt;
	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; allTenantsRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table);

	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for the current tenant only&lt;/span&gt;
	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; currentTenantRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View);
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;One thing that&amp;#39;s important to realize is that internally, NHibernate implicitly creates an entity name whether you specify one explicitly or not. The entity name it uses by default is the FullName of the mapped type. This means that passing in the Type.FullName to the methods accepting a string has the same effect as passing in the System.Type itself. This can make it easier to manage your Repository implementation as you can use the string overloads exclusively, and pass in the type name when no specific entity name is required. &lt;/p&gt;
&lt;p&gt;Now we have a repository that we can use to work with the two different profiles for our entities, as well as falling back to the standard behavior when no entity name mappings are required. This approach can be extended in any number of ways to support various complex mapping scenarios. &lt;/p&gt;
&lt;p&gt;Its worth mentioning that you can also specify the entity name via IInterceptor.GetEntityName(object), but I haven&amp;#39;t done that here because it only kicks in when saving or updating transient instances, as far as I could tell. You still have to supply the entity name to the ISession methods when querying the database, and I prefer to keep the entity name behavior in one place. &lt;/p&gt;
&lt;p&gt;If you want to use NHibernate.Linq with this approach then you will soon find that there&amp;#39;s currently (r.803) no way to specify the entity name when using the Linq&amp;lt;T&amp;gt; extension method on ISession. Not to fear, there&amp;#39;s a fairly straightforward way to provide this feature with minimal impact on any querying code you may have. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Using Entity Name with Linq to NHibernate&lt;/h2&gt;
&lt;p&gt;Lets start by adding a protected property to our Repository implementation which will provide us with an INHibernateQueryable&amp;lt;T&amp;gt;. All we need to know about that type for now is that its returned from the ISession.Linq&amp;lt;T&amp;gt; extension method. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {
			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; session.Linq&amp;lt;TEntity&amp;gt;();
		}
	}
&lt;/span&gt;
	&lt;span style="color:#008000;"&gt;//the rest of the repository is exactly the same...&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;We can now use Linq functionality in our repository by starting with that property. Lets add a useful Find() method to our repository that accepts a predicate which we can pass to the Linq implementation. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Find(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Func&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt; predicate) {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; Linq.Where(predicate).FirstOrDefault();
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;There&amp;#39;s one problem with this approach however. As you can see in the Linq property, we are passing in the current generic type of our repository. This means that our entity name mappings are not being used, and even worse than that, NHibernate will throw an error informing us that the type in question could not be found. This is to be expected, recall that we are overriding the implicit entity name, which defaults to the mapped types name, with our own. &lt;/p&gt;
&lt;p&gt;The current implementation of NHibernate.Linq is based on ICriteria, and ISession.CreateCriteria has an overload that will accept an entity name, just like all the other ISession methods. Because the entity name (or the Type) of an ICriteria can not be modified after it is created, we must control the creation of the ICriteria ourselves. The way I chose to do it was by adding a function callback to the QueryOptions object in NHibernate.Linq. This callback accepts the Linq ISession and the current expression tree and returns a new ICriteria. This lets us quickly create a delegate that will call Session.CreateCriteria(string) &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;
	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {
			&lt;/span&gt;&lt;span style="color:#008000;"&gt;//create the new ICriteria with the current entity name, as opposed to the entity type&lt;/span&gt;
			&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; queryable = Session.Linq&amp;lt;TEntity&amp;gt;();
			queryable.QueryOptions.CreateCriteraFunction = (s, e) =&amp;gt; s.CreateCriteria(GetEntityName());&lt;/span&gt;
			
			&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; queryable;
		}
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;Because we are already using this property in our Find() method above, no changes are necessary, it will pick up this functionality automatically. &lt;/p&gt;
&lt;p&gt;The changes to the NHibernate.Linq assembly are quite modest, I have included a diff in this article which you can apply to r.803 to enable this feature. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Some Gotchas&lt;/h2&gt;
&lt;p&gt;As I mentioned previously, NHibernate specifies an entity name for all entities, whether you specify one or not. If you don&amp;#39;t, the entity name is taken from the full name of the mapped type. This means that NHibernate (currently) has no way of telling whether the entity name was explicitly specified, or taken from the class. This has an interesting side effect, which I alluded to at the start of the article, and answers the question of why you cant use a &amp;#39;default&amp;#39; mapping with no explicit entity name. &lt;/p&gt;
&lt;p&gt;The internals of NHibernate still have some assumptions that an entity name will be a full type name, including namespaces. The way things are currently implemented, it will examine the entity name to see if it contains a period (&amp;#39;.&amp;#39;), and if it does, it is assumed to be a class name, if it does not, it is assumed to be a user specified entity name. This means that all the existing code that expects an entity to be identified by its type will continue to work, and therefore, this behaviour is not likely to change any time soon. &lt;/p&gt;
&lt;p&gt;So why is that important? Well, if you provide an entity name that contains a period, some sections of NHibernate (specifically &lt;a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SessionFactoryImpl.cs"&gt;SessionFactoryImpl.GetImplementors(string)&lt;/a&gt;) will assume it to be a type name and will start looking for all the mappings related to the persistent class. The upshot of this is that it will eventually find all of the mappings, regardless of their entity names that we defined, and use them both. &lt;/p&gt;
&lt;p&gt;I first noticed this when I was getting an unexpected number of results in my tests, sometimes twice as many as I was expecting. The NHibernate logs showed that there were two queries being issued, one for each of the mappings, (table and view). It turned out to be the entity names I was using: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The view specific mapping used the default (that us, unspecified) entity name: &amp;quot;My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			The table specific mapping used a mangled type name: &amp;quot;[AllTenants] My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, both of these entity names contain periods. This caused NHibernate to use both sets of mappings when performing any operation, regardless of the entity name I specified, hence the two queries being issued. The solution is to specify an explicit entity name for both mappings which contains no periods: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			View specific: &amp;quot;My_Namspace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			Table specific: &amp;quot;[AllTenants] My_Namespace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One downside of this approach, is that you must use the entity name in all references to the persistent class, i.e. in many-to-one and one-to-many elements. If you don&amp;#39;t, you will get an error when you try to create the session factory complaining about a reference to an unmapped type. Remember that we are overriding the default entity name behavior with our own, so NHibernate can no longer identify an entity using its type. In my particular case, I am generating the mappings dynamically, so this extra check is not a big problem, but if you are mapping by hand or using Fluent NHibernate, its something to be aware of. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I&amp;#39;ve talked about mapping a single class to multiple tables (or views) using the new entity name feature in the upcoming version of NHibernate. I&amp;#39;ve also provided some suggestions on working with entity specific mappings, as well as some of the things you need to be aware of when using them. Finally, I&amp;#39;ve shown how Linq to NHibernate can be adapted for use with entity names. &lt;/p&gt;
&lt;p&gt;The example given here, of tenant specific filtering, is perhaps not the best way to get the job done. Something more along the lines of &lt;a href="/wikis/howtonh/contextual-data-using-nhibernate-filters.aspx"&gt;this article&lt;/a&gt; may be more appropriate for a greenfield application. But if you find yourself dealing with a legacy database that exhibits this requirement, the new features in NHibernate 2.1 will allow you to do so with little or no impact on your domain architecture. &lt;/p&gt;
&lt;h2&gt;Patch for NHibernate.Linq r.803&lt;/h2&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs	(revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs	(working copy)
@@ -39,7 +39,12 @@
 		{
 			if (rootCriteria == null)
 			{
-				rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+				if (options.CreateCriteraFunction != null) {
+					rootCriteria = options.CreateCriteraFunction(session, expr);
+				}
+				else {
+					rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+				}
 				options.Execute(rootCriteria);
 			}
 			return expr;
Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs	(revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs	(working copy)
@@ -1,4 +1,6 @@
 using System;
+using NHibernate.Criterion;
+using NHibernate.Linq.Expressions;
 
 namespace NHibernate.Linq
 {
@@ -9,6 +11,8 @@
 	{
 		private Action&amp;lt;ICriteria&amp;gt; action;
 
+		public Func&amp;lt;ISession, QuerySourceExpression, ICriteria&amp;gt; CreateCriteraFunction;
+
 		public QueryOptions()
 		{
 			this.action = delegate { };
	&lt;/pre&gt;
&lt;/div&gt;</description></item><item><title>Mapping the same class to a view and a table using entity-name</title><link>http://nhforge.org/wikis/howtonh/mapping-the-same-class-to-a-view-and-a-table-using-entity-name/revision/3.aspx</link><pubDate>Fri, 27 Feb 2009 19:47:00 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:229</guid><dc:creator>Robert Byrne</dc:creator><description>Revision 3 posted to How to by Robert Byrne on 27/02/2009 04:47:00 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping the same class to a view and a table using entity-name&lt;/h2&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
		&lt;span style="text-decoration: line-through; color: red;"&gt;overflow-x&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;auto&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
		&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;fafafa;&lt;/span&gt;
		&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;11pt&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
		&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
	
	&lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;font&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
		&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;11pt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;important;&lt;/span&gt;
		&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;important;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;div&gt;
&lt;h2&gt;The Scenario&lt;/h2&gt;
&lt;p&gt;You have a single class which you must map to both a table and a database view (or perhaps another table). The view is returning records from the table itself, usually filtered, and the view may have more or less columns than the table. A more practical example is a multi tenant database, where a table has a tenant column, and the view filters the records to show only the ones associated with the current tenant. In this case, the backing table has a tenant column, but the view doesn&amp;#39;t. To make things even more interesting, the table is in a separate database schema. Other possible uses are dynamically mapping the same base class to multiple subclass tables, which is &lt;a href="http://www.mattfreeman.co.uk/2009/01/nhibernate-21-trunk-entity-name-some-inheritance-and-dynamic-component/"&gt;described here&lt;/a&gt;, &lt;/p&gt;
&lt;p&gt;You want to map this in NHibernate, in a way that will allow you to specify which &amp;#39;representation&amp;#39; of the entity you want to see, either the table containing records for all tenants, or the view containing records for the currently active tenant. Why? Perhaps your system needs to access all the records for all tenants to perform auditing or cleanup, things like that. In all other cases, you only want to show the records for the current tenant, via the mapped view. &lt;/p&gt;
&lt;p&gt;So, can this be done? Thankfully, yes, but only if you like living on the cutting edge! &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;In order to set this up with NHibernate, you will need to use the up and coming version 2.1, as we will be taking advantage of the new &amp;#39;entity-name&amp;#39; feature. This means you will need to checkout and build the latest version of NHibernate. There&amp;#39;s plenty of &lt;a href="/blogs/nhibernate/archive/2008/09/06/prepare-your-system-for-nhibernate.aspx"&gt;guidance online about this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this article, I&amp;#39;ll be working against version 2.1.0.1001, revision 4099. &lt;/p&gt;
&lt;p&gt;If you are using NHibernate.Linq, there&amp;#39;s some special considerations that I&amp;#39;ll explain later. You&amp;#39;ll need to modify this library to take advantage of entity names, so you will need to checkout and build it &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/"&gt;from the nhcontrib project:&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I&amp;#39;ll be working against version 1.0.0.0, revision 803. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Setting up the mappings&lt;/h2&gt;
&lt;p&gt;The class we&amp;#39;re using is fairly simple, as the magic happens in the mapping. Note that the TenantId property is nullable, as it wont be mapped when we are using the view, more on that later. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;br /&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt; {&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; Id { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }&lt;br /&gt;	&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; MyProperty { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }&lt;br /&gt;	&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;? TenantId { &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt;; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;&lt;span&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The database is also pretty straightforward, we have a table which maps to the entity class, and a view which returns records from that table. Notice that the view has no tenant id. Also notice that both of these objects are in different schemas. This just highlights the fact that the mappings for the two objects would have to be different, even if the properties were identical. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;br /&gt;CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;span&gt; [TableSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;br /&gt;&lt;/span&gt;&lt;span&gt;&lt;br /&gt;	[ID] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,&lt;br /&gt;&lt;/span&gt;&lt;span&gt;&lt;br /&gt;	[MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;200&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;NULL,&lt;br /&gt;	&lt;/span&gt;&lt;span&gt;&lt;br /&gt;	[TenantId] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;not&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;null,&lt;br /&gt;	&lt;/span&gt;&lt;span&gt;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt;&lt;span&gt; [PK_MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span&gt;[ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;br /&gt;)&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;br /&gt;CREATE&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;VIEW&lt;/span&gt;&lt;span&gt; [dbo]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity]&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;br /&gt;AS&lt;/span&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;br /&gt;SELECT&lt;/span&gt;&lt;span&gt; [ID]&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt;&lt;span&gt; [MyProperty] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span&gt; [TableSchema]&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;[MyEntity] &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;span&gt; [TenantId] &lt;/span&gt;&lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span&gt; dbo&lt;/span&gt;&lt;span style="color:#808080;"&gt;.&lt;/span&gt;&lt;span&gt;GetCurrentTenant&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;So far so good. Now we move on to the mappings. Firstly, the table specific mapping. This will allow us to access all of the records for a particular entity, regardless of the tenant they are associated with. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;br /&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Table&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`TableSchema`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;&lt;br /&gt;		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;&lt;br /&gt;		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;TenantId&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;not-null&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;&lt;br /&gt;	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;Some things to notice here are: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The &amp;#39;schema&amp;#39; attribute is set to &amp;#39;TableSchema&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;entity-name&amp;#39; attribute is specified. In this case we are keeping it fairly simple, in the real world you would want to use something that&amp;#39;s unique for each entity.
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is mapped
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest of the mapping is the exactly the same as you would expect. As I noted above, the entity name here is deliberately simplistic. Its also worth mentioning that the entity name is used to uniquely identity an entity mapping across the entire session factory, so you cant repeat it on different class mappings. Next, on to the mapping of the view, which will provide only the records for the currently active tenant. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;br /&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; ?&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;	&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;View&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;`dbo`&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;schema-action&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;none&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;			&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;native&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;&lt;br /&gt;		&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;		&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;200&lt;/span&gt;&lt;span&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt; /&amp;gt;&lt;br /&gt;	&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;This is where things get interesting: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			We have used the same table and class name, but notice that the &amp;#39;entity-name&amp;#39; is different
		&lt;/li&gt;
&lt;li&gt;
			We have a different database schema
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is absent as the view does not provide this
		&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;schema-action&amp;#39; is set to none, this will prevent NHibernate from including this mapping in its schema export, it would otherwise attempt to create a table for this view
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Although we are using the same class in both cases, the mappings can be quite different. In this example I&amp;#39;ve included the TenantId property on the entity class even though it will always be null when the view mapping is used. Whether you want to do this yourself is a matter of taste. One possibility would be to implement the TenantId property with an explicit interface, which would hide it from the consumers of the class unless needed. &lt;/p&gt;
&lt;p&gt;Now that we have the mappings we can spin up a session factory using your favourite technique. Because we have used entity names for both of our mappings, you will see that we cant just store and retrieve entities by their type as usual. The entities are no longer identified by their types, but their entity name instead. Initially, I had planned to specify the entity name only for one of the mappings, and leave the other mapping as a &amp;#39;default&amp;#39; with just its [Type] &amp;#39;name&amp;#39; specified, but as I will explain later in more detail, this wont work as expected. If you want to reliably use entity name on one mapping of the class, you must use an entity name on them all. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Entity name and the ISession&lt;/h2&gt;
&lt;p&gt;In the latest revision of NHibernate, all the methods of ISession which take a System.Type have been overloaded to accept a string specifying the entity name. Apart form this, working with entity name mappings is exactly the same as their Type counterparts. If you are using a generic implementation of IRepository, things get even easier, as you can control the entity name in a single place, for example: &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;&lt;span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;enum&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; {&lt;br /&gt;	Default,&lt;br /&gt;	Table,&lt;br /&gt;	View&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; : &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IRepository&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, TKey&amp;gt; {&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; NHibernateRepository(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode) {&lt;br /&gt;		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span&gt;.session = session;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	this&lt;/span&gt;&lt;span&gt;.mode = mode;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Get(TKey key) {&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; (TEntity)session.Get(GetEntityName(), key);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; List() {&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span&gt; session.CreateCriteria(GetEntityName()).List&amp;lt;TEntity&amp;gt;();&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;void&lt;/span&gt;&lt;span&gt; SaveOrUpdate(TEntity entity) {&lt;br /&gt;		session.SaveOrUpdate(GetEntityName(), entity);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&lt;span&gt; GetEntityName() {&lt;br /&gt;		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;switch&lt;/span&gt;&lt;span&gt; (mode) {&lt;br /&gt;			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;Table&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;br /&gt;			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;case&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#a31515;"&gt;&amp;quot;View&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;br /&gt;			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;default&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span&gt;(TEntity).FullName;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span&gt; session;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt; mode;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;The entity name to use is based on the parameter we pass into the repository. In the real world you will want to use something more sophisticated to control the entity names. By specifying the Table mode when creating this repository, all the operations we perform against it will now deal with the records for all tenants. If we only want to deal with the records for a specific tenant, then we pass in the View mode. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for all tenants&lt;/span&gt;&lt;br /&gt;	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; allTenantsRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.Table);&lt;br /&gt;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#008000;"&gt;//this repository will work with data for the current tenant only&lt;/span&gt;&lt;br /&gt;	&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; currentTenantRepository = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span&gt;.View);&lt;br /&gt;	&lt;/span&gt;&lt;br /&gt;	&lt;/pre&gt;
&lt;p&gt;One thing that&amp;#39;s important to realize is that internally, NHibernate implicitly creates an entity name whether you specify one explicitly or not. The entity name it uses by default is the FullName of the mapped type. This means that passing in the Type.FullName to the methods accepting a string has the same effect as passing in the System.Type itself. This can make it easier to manage your Repository implementation as you can use the string overloads exclusively, and pass in the type name when no specific entity name is required. &lt;/p&gt;
&lt;p&gt;Now we have a repository that we can use to work with the two different profiles for our entities, as well as falling back to the standard behavior when no entity name mappings are required. This approach can be extended in any number of ways to support various complex mapping scenarios. &lt;/p&gt;
&lt;p&gt;Its worth mentioning that you can also specify the entity name via IInterceptor.GetEntityName(object), but I haven&amp;#39;t done that here because it only kicks in when saving or updating transient instances, as far as I could tell. You still have to supply the entity name to the ISession methods when querying the database, and I prefer to keep the entity name behavior in one place. &lt;/p&gt;
&lt;p&gt;If you want to use NHibernate.Linq with this approach then you will soon find that there&amp;#39;s currently (r.803) no way to specify the entity name when using the Linq&amp;lt;T&amp;gt; extension method on ISession. Not to fear, there&amp;#39;s a fairly straightforward way to provide this feature with minimal impact on any querying code you may have. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Using Entity Name with Linq to NHibernate&lt;/h2&gt;
&lt;p&gt;Lets start by adding a protected property to our Repository implementation which will provide us with an INHibernateQueryable&amp;lt;T&amp;gt;. All we need to know about that type for now is that its returned from the ISession.Linq&amp;lt;T&amp;gt; extension method. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {&lt;br /&gt;		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {&lt;br /&gt;			&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; session.Linq&amp;lt;TEntity&amp;gt;();&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;	&lt;span style="color:#008000;"&gt;//the rest of the repository is exactly the same...&lt;/span&gt;&lt;br /&gt;	&lt;/pre&gt;
&lt;p&gt;We can now use Linq functionality in our repository by starting with that property. Lets add a useful Find() method to our repository that accepts a predicate which we can pass to the Linq implementation. &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span&gt; TEntity Find(&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Expression&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Func&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;gt; predicate) {&lt;br /&gt;		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; Linq.Where(predicate).FirstOrDefault();&lt;br /&gt;	}&lt;br /&gt;	&lt;/span&gt;&lt;br /&gt;	&lt;/pre&gt;
&lt;p&gt;There&amp;#39;s one problem with this approach however. As you can see in the Linq property, we are passing in the current generic type of our repository. This means that our entity name mappings are not being used, and even worse than that, NHibernate will throw an error informing us that the type in question could not be found. This is to be expected, recall that we are overriding the implicit entity name, which defaults to the mapped types name, with our own. &lt;/p&gt;
&lt;p&gt;The current implementation of NHibernate.Linq is based on ICriteria, and ISession.CreateCriteria has an overload that will accept an entity name, just like all the other ISession methods. Because the entity name (or the Type) of an ICriteria can not be modified after it is created, we must control the creation of the ICriteria ourselves. The way I chose to do it was by adding a function callback to the QueryOptions object in NHibernate.Linq. This callback accepts the Linq ISession and the current expression tree and returns a new ICriteria. This lets us quickly create a delegate that will call Session.CreateCriteria(string) &lt;/p&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;	&lt;span&gt;&lt;br /&gt;	&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span&gt;&amp;lt;TEntity&amp;gt; Linq {&lt;br /&gt;		&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;&lt;span&gt; {&lt;br /&gt;			&lt;/span&gt;&lt;span style="color:#008000;"&gt;//create the new ICriteria with the current entity name, as opposed to the entity type&lt;/span&gt;&lt;br /&gt;			&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt;&lt;span&gt; queryable = Session.Linq&amp;lt;TEntity&amp;gt;();&lt;br /&gt;			queryable.QueryOptions.CreateCriteraFunction = (s, e) =&amp;gt; s.CreateCriteria(GetEntityName());&lt;/span&gt;&lt;br /&gt;			&lt;br /&gt;			&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span&gt; queryable;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	&lt;/span&gt;&lt;br /&gt;	&lt;/pre&gt;
&lt;p&gt;Because we are already using this property in our Find() method above, no changes are necessary, it will pick up this functionality automatically. &lt;/p&gt;
&lt;p&gt;The changes to the NHibernate.Linq assembly are quite modest, I have included a diff in this article which you can apply to r.803 to enable this feature. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Some Gotchas&lt;/h2&gt;
&lt;p&gt;As I mentioned previously, NHibernate specifies an entity name for all entities, whether you specify one or not. If you don&amp;#39;t, the entity name is taken from the full name of the mapped type. This means that NHibernate (currently) has no way of telling whether the entity name was explicitly specified, or taken from the class. This has an interesting side effect, which I alluded to at the start of the article, and answers the question of why you cant use a &amp;#39;default&amp;#39; mapping with no explicit entity name. &lt;/p&gt;
&lt;p&gt;The internals of NHibernate still have some assumptions that an entity name will be a full type name, including namespaces. The way things are currently implemented, it will examine the entity name to see if it contains a period (&amp;#39;.&amp;#39;), and if it does, it is assumed to be a class name, if it does not, it is assumed to be a user specified entity name. This means that all the existing code that expects an entity to be identified by its type will continue to work, and therefore, this behaviour is not likely to change any time soon. &lt;/p&gt;
&lt;p&gt;So why is that important? Well, if you provide an entity name that contains a period, some sections of NHibernate (specifically &lt;a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SessionFactoryImpl.cs"&gt;SessionFactoryImpl.GetImplementors(string)&lt;/a&gt;) will assume it to be a type name and will start looking for all the mappings related to the persistent class. The upshot of this is that it will eventually find all of the mappings, regardless of their entity names that we defined, and use them both. &lt;/p&gt;
&lt;p&gt;I first noticed this when I was getting an unexpected number of results in my tests, sometimes twice as many as I was expecting. The NHibernate logs showed that there were two queries being issued, one for each of the mappings, (table and view). It turned out to be the entity names I was using: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The view specific mapping used the default (that us, unspecified) entity name: &amp;quot;My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			The table specific mapping used a mangled type name: &amp;quot;[AllTenants] My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, both of these entity names contain periods. This caused NHibernate to use both sets of mappings when performing any operation, regardless of the entity name I specified, hence the two queries being issued. The solution is to specify an explicit entity name for both mappings which contains no periods: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			View specific: &amp;quot;My_Namspace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			Table specific: &amp;quot;[AllTenants] My_Namespace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One downside of this approach, is that you must use the entity name in all references to the persistent class, i.e. in many-to-one and one-to-many elements. If you don&amp;#39;t, you will get an error when you try to create the session factory complaining about a reference to an unmapped type. Remember that we are overriding the default entity name behavior with our own, so NHibernate can no longer identify an entity using its type. In my particular case, I am generating the mappings dynamically, so this extra check is not a big problem, but if you are mapping by hand or using Fluent NHibernate, its something to be aware of. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I&amp;#39;ve talked about mapping a single class to multiple tables (or views) using the new entity name feature in the upcoming version of NHibernate. I&amp;#39;ve also provided some suggestions on working with entity specific mappings, as well as some of the things you need to be aware of when using them. Finally, I&amp;#39;ve shown how Linq to NHibernate can be adapted for use with entity names. &lt;/p&gt;
&lt;p&gt;The example given here, of tenant specific filtering, is perhaps not the best way to get the job done. Something more along the lines of &lt;a href="/wikis/howtonh/contextual-data-using-nhibernate-filters.aspx"&gt;this article&lt;/a&gt; may be more appropriate for a greenfield application. But if you find yourself dealing with a legacy database that exhibits this requirement, the new features in NHibernate 2.1 will allow you to do so with little or no impact on your domain architecture. &lt;/p&gt;
&lt;h2&gt;Patch for NHibernate.Linq r.803&lt;/h2&gt;
&lt;pre style="overflow-x:auto;background-color:#fafafa;font-family:monospace;"&gt;Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs&lt;br /&gt;===================================================================&lt;br /&gt;--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs (revision 803)&lt;br /&gt;+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs (working copy)&lt;br /&gt;@@ -39,7 +39,12 @@&lt;br /&gt;{&lt;br /&gt;if (rootCriteria == null)&lt;br /&gt;{&lt;br /&gt;- rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);&lt;br /&gt;+ if (options.CreateCriteraFunction != null) {&lt;br /&gt;+ rootCriteria = options.CreateCriteraFunction(session, expr);&lt;br /&gt;+ }&lt;br /&gt;+ else {&lt;br /&gt;+ rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);&lt;br /&gt;+ }&lt;br /&gt;options.Execute(rootCriteria);&lt;br /&gt;}&lt;br /&gt;return expr;&lt;br /&gt;Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs&lt;br /&gt;===================================================================&lt;br /&gt;--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs (revision 803)&lt;br /&gt;+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs (working copy)&lt;br /&gt;@@ -1,4 +1,6 @@&lt;br /&gt;using System;&lt;br /&gt;+using NHibernate.Criterion;&lt;br /&gt;+using NHibernate.Linq.Expressions;&lt;br /&gt;&lt;br /&gt;namespace NHibernate.Linq&lt;br /&gt;{&lt;br /&gt;@@ -9,6 +11,8 @@&lt;br /&gt;{&lt;br /&gt;private Action&amp;lt;ICriteria&amp;gt; action;&lt;br /&gt;&lt;br /&gt;+ public Func&amp;lt;ISession, QuerySourceExpression, ICriteria&amp;gt; CreateCriteraFunction;&lt;br /&gt;+&lt;br /&gt;public QueryOptions()&lt;br /&gt;{&lt;br /&gt;this.action = delegate { };&lt;br /&gt;	&lt;/pre&gt;
&lt;/div&gt;</description></item><item><title>Mapping the same class to a view and a table using entity-name</title><link>http://nhforge.org/wikis/howtonh/mapping-the-same-class-to-a-view-and-a-table-using-entity-name/revision/2.aspx</link><pubDate>Fri, 27 Feb 2009 19:38:18 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:228</guid><dc:creator>Robert Byrne</dc:creator><description>Revision 2 posted to How to by Robert Byrne on 27/02/2009 04:38:18 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping the same class to a view and a table using entity-name&lt;/h2&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
	pre {
		overflow-x: auto;
		background-color: #fafafa;
		font-size: 11pt;
		font-family: monospace;
	}
	
	pre font {
		font-size: 11pt !important;
		font-family: monospace !important;
	}
}
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;div&gt;
&lt;h2&gt;The Scenario&lt;/h2&gt;
&lt;p&gt;You have a single class which you must map to both a table and a database view (or perhaps another table). The view is returning records from the table itself, usually filtered, and the view may have more or less columns than the table. A more practical example is a multi tenant database, where a table has a tenant column, and the view filters the records to show only the ones associated with the current tenant. In this case, the backing table has a tenant column, but the view doesn&amp;#39;t. To make things even more interesting, the table is in a separate database schema. Other possible uses are dynamically mapping the same base class to multiple subclass tables, which is &lt;a href="http://www.mattfreeman.co.uk/2009/01/nhibernate-21-trunk-entity-name-some-inheritance-and-dynamic-component/"&gt;described here&lt;/a&gt;, &lt;/p&gt;
&lt;p&gt;You want to map this in NHibernate, in a way that will allow you to specify which &amp;#39;representation&amp;#39; of the entity you want to see, either the table containing records for all tenants, or the view containing records for the currently active tenant. Why? Perhaps your system needs to access all the records for all tenants to perform auditing or cleanup, things like that. In all other cases, you only want to show the records for the current tenant, via the mapped view. &lt;/p&gt;
&lt;p&gt;So, can this be done? Thankfully, yes, but only if you like living on the cutting edge! &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;In order to set this up with NHibernate, you will need to use the up and coming version 2.1, as we will be taking advantage of the new &amp;#39;entity-name&amp;#39; feature. This means you will need to checkout and build the latest version of NHibernate. There&amp;#39;s plenty of &lt;a href="/blogs/nhibernate/archive/2008/09/06/prepare-your-system-for-nhibernate.aspx"&gt;guidance online about this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this article, I&amp;#39;ll be working against version 2.1.0.1001, revision 4099. &lt;/p&gt;
&lt;p&gt;If you are using NHibernate.Linq, there&amp;#39;s some special considerations that I&amp;#39;ll explain later. You&amp;#39;ll need to modify this library to take advantage of entity names, so you will need to checkout and build it &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/"&gt;from the nhcontrib project:&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I&amp;#39;ll be working against version 1.0.0.0, revision 803. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Setting up the mappings&lt;/h2&gt;
&lt;p&gt;The class we&amp;#39;re using is fairly simple, as the magic happens in the mapping. Note that the TenantId property is nullable, as it wont be mapped when we are using the view, more on that later. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; Id { &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; }
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; MyProperty { &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; }
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;? TenantId { &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The database is also pretty straightforward, we have a table which maps to the entity class, and a view which returns records from that table. Notice that the view has no tenant id. Also notice that both of these objects are in different schemas. This just highlights the fact that the mappings for the two objects would have to be different, even if the properties were identical. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [TableSchema]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[MyEntity]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;(
&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	[ID] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;not&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;null,
&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	[MyProperty] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;(&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;200&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;NULL,
	&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	[TenantId] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;not&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;null,
	&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [PK_MyEntity] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;PRIMARY&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;KEY&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;CLUSTERED&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;(&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[ID]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;)
)
&lt;/span&gt;

&lt;span style="font-size:x-small;color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;VIEW&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [dbo]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[MyEntity]
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
AS&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
SELECT&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [ID]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;,&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [MyProperty] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [TableSchema]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[MyEntity] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [TenantId] &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; dbo&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;GetCurrentTenant&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;()
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;So far so good. Now we move on to the mappings. Firstly, the table specific mapping. This will allow us to access all of the records for a particular entity, regardless of the tenant they are associated with. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;Table&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`TableSchema`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;native&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;200&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;TenantId&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;not-null&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Some things to notice here are: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The &amp;#39;schema&amp;#39; attribute is set to &amp;#39;TableSchema&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;entity-name&amp;#39; attribute is specified. In this case we are keeping it fairly simple, in the real world you would want to use something that&amp;#39;s unique for each entity.
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is mapped
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest of the mapping is the exactly the same as you would expect. As I noted above, the entity name here is deliberately simplistic. Its also worth mentioning that the entity name is used to uniquely identity an entity mapping across the entire session factory, so you cant repeat it on different class mappings. Next, on to the mapping of the view, which will provide only the records for the currently active tenant. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;View&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`dbo`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;schema-action&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;none&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;native&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;200&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;This is where things get interesting: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			We have used the same table and class name, but notice that the &amp;#39;entity-name&amp;#39; is different
		&lt;/li&gt;
&lt;li&gt;
			We have a different database schema
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is absent as the view does not provide this
		&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;schema-action&amp;#39; is set to none, this will prevent NHibernate from including this mapping in its schema export, it would otherwise attempt to create a table for this view
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Although we are using the same class in both cases, the mappings can be quite different. In this example I&amp;#39;ve included the TenantId property on the entity class even though it will always be null when the view mapping is used. Whether you want to do this yourself is a matter of taste. One possibility would be to implement the TenantId property with an explicit interface, which would hide it from the consumers of the class unless needed. &lt;/p&gt;
&lt;p&gt;Now that we have the mappings we can spin up a session factory using your favourite technique. Because we have used entity names for both of our mappings, you will see that we cant just store and retrieve entities by their type as usual. The entities are no longer identified by their types, but their entity name instead. Initially, I had planned to specify the entity name only for one of the mappings, and leave the other mapping as a &amp;#39;default&amp;#39; with just its [Type] &amp;#39;name&amp;#39; specified, but as I will explain later in more detail, this wont work as expected. If you want to reliably use entity name on one mapping of the class, you must use an entity name on them all. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Entity name and the ISession&lt;/h2&gt;
&lt;p&gt;In the latest revision of NHibernate, all the methods of ISession which take a System.Type have been overloaded to accept a string specifying the entity name. Apart form this, working with entity name mappings is exactly the same as their Type counterparts. If you are using a generic implementation of IRepository, things get even easier, as you can control the entity name in a single place, for example: &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;"&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;enum&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
	Default,
	Table,
	View
}

&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity, TKey&amp;gt; : &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;IRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity, TKey&amp;gt; {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; NHibernateRepository(&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session, &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; mode) {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.session = session;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;	this&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.mode = mode;
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; TEntity Get(TKey key) {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; (TEntity)session.Get(GetEntityName(), key);
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity&amp;gt; List() {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session.CreateCriteria(GetEntityName()).List&amp;lt;TEntity&amp;gt;();
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; SaveOrUpdate(TEntity entity) {
		session.SaveOrUpdate(GetEntityName(), entity);
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; GetEntityName() {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;switch&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; (mode) {
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Table: &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;&amp;quot;Table&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.View: &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;&amp;quot;View&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;default&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;: &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;(TEntity).FullName;
		}
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; mode;
}
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;The entity name to use is based on the parameter we pass into the repository. In the real world you will want to use something more sophisticated to control the entity names. By specifying the Table mode when creating this repository, all the operations we perform against it will now deal with the records for all tenants. If we only want to deal with the records for a specific tenant, then we pass in the View mode. &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#008000;"&gt;//this repository will work with data for all tenants&lt;/span&gt;
	&lt;span style="font-size:x-small;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; allTenantsRepository = &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;, &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Table);

	&lt;/span&gt;&lt;span style="font-size:x-small;color:#008000;"&gt;//this repository will work with data for the current tenant only&lt;/span&gt;
	&lt;span style="font-size:x-small;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; currentTenantRepository = &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;, &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.View);
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;One thing that&amp;#39;s important to realize is that internally, NHibernate implicitly creates an entity name whether you specify one explicitly or not. The entity name it uses by default is the FullName of the mapped type. This means that passing in the Type.FullName to the methods accepting a string has the same effect as passing in the System.Type itself. This can make it easier to manage your Repository implementation as you can use the string overloads exclusively, and pass in the type name when no specific entity name is required. &lt;/p&gt;
&lt;p&gt;Now we have a repository that we can use to work with the two different profiles for our entities, as well as falling back to the standard behavior when no entity name mappings are required. This approach can be extended in any number of ways to support various complex mapping scenarios. &lt;/p&gt;
&lt;p&gt;Its worth mentioning that you can also specify the entity name via IInterceptor.GetEntityName(object), but I haven&amp;#39;t done that here because it only kicks in when saving or updating transient instances, as far as I could tell. You still have to supply the entity name to the ISession methods when querying the database, and I prefer to keep the entity name behavior in one place. &lt;/p&gt;
&lt;p&gt;If you want to use NHibernate.Linq with this approach then you will soon find that there&amp;#39;s currently (r.803) no way to specify the entity name when using the Linq&amp;lt;T&amp;gt; extension method on ISession. Not to fear, there&amp;#39;s a fairly straightforward way to provide this feature with minimal impact on any querying code you may have. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Using Entity Name with Linq to NHibernate&lt;/h2&gt;
&lt;p&gt;Lets start by adding a protected property to our Repository implementation which will provide us with an INHibernateQueryable&amp;lt;T&amp;gt;. All we need to know about that type for now is that its returned from the ISession.Linq&amp;lt;T&amp;gt; extension method. &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session.Linq&amp;lt;TEntity&amp;gt;();
		}
	}
&lt;/span&gt;
	&lt;span style="font-size:x-small;color:#008000;"&gt;//the rest of the repository is exactly the same...&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;We can now use Linq functionality in our repository by starting with that property. Lets add a useful Find() method to our repository that accepts a predicate which we can pass to the Linq implementation. &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; TEntity Find(&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;Func&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity, &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;gt;&amp;gt; predicate) {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; Linq.Where(predicate).FirstOrDefault();
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;There&amp;#39;s one problem with this approach however. As you can see in the Linq property, we are passing in the current generic type of our repository. This means that our entity name mappings are not being used, and even worse than that, NHibernate will throw an error informing us that the type in question could not be found. This is to be expected, recall that we are overriding the implicit entity name, which defaults to the mapped types name, with our own. &lt;/p&gt;
&lt;p&gt;The current implementation of NHibernate.Linq is based on ICriteria, and ISession.CreateCriteria has an overload that will accept an entity name, just like all the other ISession methods. Because the entity name (or the Type) of an ICriteria can not be modified after it is created, we must control the creation of the ICriteria ourselves. The way I chose to do it was by adding a function callback to the QueryOptions object in NHibernate.Linq. This callback accepts the Linq ISession and the current expression tree and returns a new ICriteria. This lets us quickly create a delegate that will call Session.CreateCriteria(string) &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#008000;"&gt;//create the new ICriteria with the current entity name, as opposed to the entity type&lt;/span&gt;
			&lt;span style="font-size:x-small;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; queryable = Session.Linq&amp;lt;TEntity&amp;gt;();
			queryable.QueryOptions.CreateCriteraFunction = (s, e) =&amp;gt; s.CreateCriteria(GetEntityName());&lt;/span&gt;
			
			&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; queryable;
		}
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;Because we are already using this property in our Find() method above, no changes are necessary, it will pick up this functionality automatically. &lt;/p&gt;
&lt;p&gt;The changes to the NHibernate.Linq assembly are quite modest, I have included a diff in this article which you can apply to r.803 to enable this feature. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Some Gotchas&lt;/h2&gt;
&lt;p&gt;As I mentioned previously, NHibernate specifies an entity name for all entities, whether you specify one or not. If you don&amp;#39;t, the entity name is taken from the full name of the mapped type. This means that NHibernate (currently) has no way of telling whether the entity name was explicitly specified, or taken from the class. This has an interesting side effect, which I alluded to at the start of the article, and answers the question of why you cant use a &amp;#39;default&amp;#39; mapping with no explicit entity name. &lt;/p&gt;
&lt;p&gt;The internals of NHibernate still have some assumptions that an entity name will be a full type name, including namespaces. The way things are currently implemented, it will examine the entity name to see if it contains a period (&amp;#39;.&amp;#39;), and if it does, it is assumed to be a class name, if it does not, it is assumed to be a user specified entity name. This means that all the existing code that expects an entity to be identified by its type will continue to work, and therefore, this behaviour is not likely to change any time soon. &lt;/p&gt;
&lt;p&gt;So why is that important? Well, if you provide an entity name that contains a period, some sections of NHibernate (specifically &lt;a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SessionFactoryImpl.cs"&gt;SessionFactoryImpl.GetImplementors(string)&lt;/a&gt;) will assume it to be a type name and will start looking for all the mappings related to the persistent class. The upshot of this is that it will eventually find all of the mappings, regardless of their entity names that we defined, and use them both. &lt;/p&gt;
&lt;p&gt;I first noticed this when I was getting an unexpected number of results in my tests, sometimes twice as many as I was expecting. The NHibernate logs showed that there were two queries being issued, one for each of the mappings, (table and view). It turned out to be the entity names I was using: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The view specific mapping used the default (that us, unspecified) entity name: &amp;quot;My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			The table specific mapping used a mangled type name: &amp;quot;[AllTenants] My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, both of these entity names contain periods. This caused NHibernate to use both sets of mappings when performing any operation, regardless of the entity name I specified, hence the two queries being issued. The solution is to specify an explicit entity name for both mappings which contains no periods: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			View specific: &amp;quot;My_Namspace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			Table specific: &amp;quot;[AllTenants] My_Namespace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One downside of this approach, is that you must use the entity name in all references to the persistent class, i.e. in many-to-one and one-to-many elements. If you don&amp;#39;t, you will get an error when you try to create the session factory complaining about a reference to an unmapped type. Remember that we are overriding the default entity name behavior with our own, so NHibernate can no longer identify an entity using its type. In my particular case, I am generating the mappings dynamically, so this extra check is not a big problem, but if you are mapping by hand or using Fluent NHibernate, its something to be aware of. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I&amp;#39;ve talked about mapping a single class to multiple tables (or views) using the new entity name feature in the upcoming version of NHibernate. I&amp;#39;ve also provided some suggestions on working with entity specific mappings, as well as some of the things you need to be aware of when using them. Finally, I&amp;#39;ve shown how Linq to NHibernate can be adapted for use with entity names. &lt;/p&gt;
&lt;p&gt;The example given here, of tenant specific filtering, is perhaps not the best way to get the job done. Something more along the lines of &lt;a href="/wikis/howtonh/contextual-data-using-nhibernate-filters.aspx"&gt;this article&lt;/a&gt; may be more appropriate for a greenfield application. But if you find yourself dealing with a legacy database that exhibits this requirement, the new features in NHibernate 2.1 will allow you to do so with little or no impact on your domain architecture. &lt;/p&gt;
&lt;h2&gt;Patch for NHibernate.Linq r.803&lt;/h2&gt;
&lt;pre&gt;Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs (revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs (working copy)
@@ -39,7 +39,12 @@
{
if (rootCriteria == null)
{
- rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+ if (options.CreateCriteraFunction != null) {
+ rootCriteria = options.CreateCriteraFunction(session, expr);
+ }
+ else {
+ rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+ }
options.Execute(rootCriteria);
}
return expr;
Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs (revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs (working copy)
@@ -1,4 +1,6 @@
using System;
+using NHibernate.Criterion;
+using NHibernate.Linq.Expressions;

namespace NHibernate.Linq
{
@@ -9,6 +11,8 @@
{
private Action&amp;lt;ICriteria&amp;gt; action;

+ public Func&amp;lt;ISession, QuerySourceExpression, ICriteria&amp;gt; CreateCriteraFunction;
+
public QueryOptions()
{
this.action = delegate { };
	&lt;/pre&gt;
&lt;/div&gt;</description></item><item><title>Mapping the same class to a view and a table using entity-name</title><link>http://nhforge.org/wikis/howtonh/mapping-the-same-class-to-a-view-and-a-table-using-entity-name/revision/1.aspx</link><pubDate>Fri, 27 Feb 2009 19:36:21 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:227</guid><dc:creator>Robert Byrne</dc:creator><description>Revision 1 posted to How to by Robert Byrne on 27/02/2009 04:36:21 p.m.&lt;br /&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
	pre {
		overflow-x: auto;
		background-color: #fafafa;
		font-size: 11pt;
		font-family: monospace;
	}
	
	pre font {
		font-size: 11pt !important;
		font-family: monospace !important;
	}
}
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;div&gt;
&lt;h2&gt;The Scenario&lt;/h2&gt;
&lt;p&gt;You have a single class which you must map to both a table and a database view (or perhaps another table). The view is returning records from the table itself, usually filtered, and the view may have more or less columns than the table. A more practical example is a multi tenant database, where a table has a tenant column, and the view filters the records to show only the ones associated with the current tenant. In this case, the backing table has a tenant column, but the view doesn&amp;#39;t. To make things even more interesting, the table is in a separate database schema. Other possible uses are dynamically mapping the same base class to multiple subclass tables, which is &lt;a href="http://www.mattfreeman.co.uk/2009/01/nhibernate-21-trunk-entity-name-some-inheritance-and-dynamic-component/"&gt;described here&lt;/a&gt;, &lt;/p&gt;
&lt;p&gt;You want to map this in NHibernate, in a way that will allow you to specify which &amp;#39;representation&amp;#39; of the entity you want to see, either the table containing records for all tenants, or the view containing records for the currently active tenant. Why? Perhaps your system needs to access all the records for all tenants to perform auditing or cleanup, things like that. In all other cases, you only want to show the records for the current tenant, via the mapped view. &lt;/p&gt;
&lt;p&gt;So, can this be done? Thankfully, yes, but only if you like living on the cutting edge! &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;In order to set this up with NHibernate, you will need to use the up and coming version 2.1, as we will be taking advantage of the new &amp;#39;entity-name&amp;#39; feature. This means you will need to checkout and build the latest version of NHibernate. There&amp;#39;s plenty of &lt;a href="/blogs/nhibernate/archive/2008/09/06/prepare-your-system-for-nhibernate.aspx"&gt;guidance online about this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this article, I&amp;#39;ll be working against version 2.1.0.1001, revision 4099. &lt;/p&gt;
&lt;p&gt;If you are using NHibernate.Linq, there&amp;#39;s some special considerations that I&amp;#39;ll explain later. You&amp;#39;ll need to modify this library to take advantage of entity names, so you will need to checkout and build it &lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Linq/"&gt;from the nhcontrib project:&lt;/a&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I&amp;#39;ll be working against version 1.0.0.0, revision 803. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Setting up the mappings&lt;/h2&gt;
&lt;p&gt;The class we&amp;#39;re using is fairly simple, as the magic happens in the mapping. Note that the TenantId property is nullable, as it wont be mapped when we are using the view, more on that later. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; Id { &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; }
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; MyProperty { &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; }
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;virtual&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;? TenantId { &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The database is also pretty straightforward, we have a table which maps to the entity class, and a view which returns records from that table. Notice that the view has no tenant id. Also notice that both of these objects are in different schemas. This just highlights the fact that the mappings for the two objects would have to be different, even if the properties were identical. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [TableSchema]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[MyEntity]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;(
&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	[ID] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;not&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;null,
&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	[MyProperty] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;(&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;200&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;NULL,
	&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	[TenantId] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;not&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;null,
	&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [PK_MyEntity] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;PRIMARY&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;KEY&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;CLUSTERED&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;(&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[ID]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;)
)
&lt;/span&gt;

&lt;span style="font-size:x-small;color:#0000ff;"&gt;
CREATE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;VIEW&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [dbo]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[MyEntity]
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
AS&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
SELECT&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [ID]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;,&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [MyProperty] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;FROM&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [TableSchema]&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;[MyEntity] &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;WHERE&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; [TenantId] &lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; dbo&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;.&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;GetCurrentTenant&lt;/span&gt;&lt;span style="font-size:x-small;color:#808080;"&gt;()
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;So far so good. Now we move on to the mappings. Firstly, the table specific mapping. This will allow us to access all of the records for a particular entity, regardless of the tenant they are associated with. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;Table&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`TableSchema`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;native&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;200&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;TenantId&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;not-null&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Some things to notice here are: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The &amp;#39;schema&amp;#39; attribute is set to &amp;#39;TableSchema&amp;#39;&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;entity-name&amp;#39; attribute is specified. In this case we are keeping it fairly simple, in the real world you would want to use something that&amp;#39;s unique for each entity.
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is mapped
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The rest of the mapping is the exactly the same as you would expect. As I noted above, the entity name here is deliberately simplistic. Its also worth mentioning that the entity name is used to uniquely identity an entity mapping across the entire session factory, so you cant repeat it on different class mappings. Next, on to the mapping of the view, which will provide only the records for the currently active tenant. &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;
&amp;lt;?&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;xml&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;1.0&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;utf-8&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; ?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyAssembly.MyNamespace&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
	&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;table&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`MyEntity`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;entity-name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;View&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;schema&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;`dbo`&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;schema-action&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;none&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;Id&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
			&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;generator&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;native&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
		&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;id&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
		&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;MyProperty&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;length&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;200&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt; /&amp;gt;
	&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;This is where things get interesting: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			We have used the same table and class name, but notice that the &amp;#39;entity-name&amp;#39; is different
		&lt;/li&gt;
&lt;li&gt;
			We have a different database schema
		&lt;/li&gt;
&lt;li&gt;
			The TenantId property is absent as the view does not provide this
		&lt;/li&gt;
&lt;li&gt;
			The new &amp;#39;schema-action&amp;#39; is set to none, this will prevent NHibernate from including this mapping in its schema export, it would otherwise attempt to create a table for this view
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Although we are using the same class in both cases, the mappings can be quite different. In this example I&amp;#39;ve included the TenantId property on the entity class even though it will always be null when the view mapping is used. Whether you want to do this yourself is a matter of taste. One possibility would be to implement the TenantId property with an explicit interface, which would hide it from the consumers of the class unless needed. &lt;/p&gt;
&lt;p&gt;Now that we have the mappings we can spin up a session factory using your favourite technique. Because we have used entity names for both of our mappings, you will see that we cant just store and retrieve entities by their type as usual. The entities are no longer identified by their types, but their entity name instead. Initially, I had planned to specify the entity name only for one of the mappings, and leave the other mapping as a &amp;#39;default&amp;#39; with just its [Type] &amp;#39;name&amp;#39; specified, but as I will explain later in more detail, this wont work as expected. If you want to reliably use entity name on one mapping of the class, you must use an entity name on them all. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Entity name and the ISession&lt;/h2&gt;
&lt;p&gt;In the latest revision of NHibernate, all the methods of ISession which take a System.Type have been overloaded to accept a string specifying the entity name. Apart form this, working with entity name mappings is exactly the same as their Type counterparts. If you are using a generic implementation of IRepository, things get even easier, as you can control the entity name in a single place, for example: &lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:x-small;"&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;enum&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
	Default,
	Table,
	View
}

&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity, TKey&amp;gt; : &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;IRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity, TKey&amp;gt; {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; NHibernateRepository(&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session, &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; mode) {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.session = session;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;	this&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.mode = mode;
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; TEntity Get(TKey key) {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; (TEntity)session.Get(GetEntityName(), key);
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity&amp;gt; List() {
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;	return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session.CreateCriteria(GetEntityName()).List&amp;lt;TEntity&amp;gt;();
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; SaveOrUpdate(TEntity entity) {
		session.SaveOrUpdate(GetEntityName(), entity);
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; GetEntityName() {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;switch&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; (mode) {
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Table: &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;&amp;quot;Table&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;case&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.View: &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#a31515;"&gt;&amp;quot;View&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;default&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;: &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;typeof&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;(TEntity).FullName;
		}
	}
	
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;ISession&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; mode;
}
&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;The entity name to use is based on the parameter we pass into the repository. In the real world you will want to use something more sophisticated to control the entity names. By specifying the Table mode when creating this repository, all the operations we perform against it will now deal with the records for all tenants. If we only want to deal with the records for a specific tenant, then we pass in the View mode. &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#008000;"&gt;//this repository will work with data for all tenants&lt;/span&gt;
	&lt;span style="font-size:x-small;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; allTenantsRepository = &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;, &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Table);

	&lt;/span&gt;&lt;span style="font-size:x-small;color:#008000;"&gt;//this repository will work with data for the current tenant only&lt;/span&gt;
	&lt;span style="font-size:x-small;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; currentTenantRepository = &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;NHibernateRepository&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;MyEntity&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;, &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;gt;(session, &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;EntityNameMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.View);
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;One thing that&amp;#39;s important to realize is that internally, NHibernate implicitly creates an entity name whether you specify one explicitly or not. The entity name it uses by default is the FullName of the mapped type. This means that passing in the Type.FullName to the methods accepting a string has the same effect as passing in the System.Type itself. This can make it easier to manage your Repository implementation as you can use the string overloads exclusively, and pass in the type name when no specific entity name is required. &lt;/p&gt;
&lt;p&gt;Now we have a repository that we can use to work with the two different profiles for our entities, as well as falling back to the standard behavior when no entity name mappings are required. This approach can be extended in any number of ways to support various complex mapping scenarios. &lt;/p&gt;
&lt;p&gt;Its worth mentioning that you can also specify the entity name via IInterceptor.GetEntityName(object), but I haven&amp;#39;t done that here because it only kicks in when saving or updating transient instances, as far as I could tell. You still have to supply the entity name to the ISession methods when querying the database, and I prefer to keep the entity name behavior in one place. &lt;/p&gt;
&lt;p&gt;If you want to use NHibernate.Linq with this approach then you will soon find that there&amp;#39;s currently (r.803) no way to specify the entity name when using the Linq&amp;lt;T&amp;gt; extension method on ISession. Not to fear, there&amp;#39;s a fairly straightforward way to provide this feature with minimal impact on any querying code you may have. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Using Entity Name with Linq to NHibernate&lt;/h2&gt;
&lt;p&gt;Lets start by adding a protected property to our Repository implementation which will provide us with an INHibernateQueryable&amp;lt;T&amp;gt;. All we need to know about that type for now is that its returned from the ISession.Linq&amp;lt;T&amp;gt; extension method. &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; session.Linq&amp;lt;TEntity&amp;gt;();
		}
	}
&lt;/span&gt;
	&lt;span style="font-size:x-small;color:#008000;"&gt;//the rest of the repository is exactly the same...&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;We can now use Linq functionality in our repository by starting with that property. Lets add a useful Find() method to our repository that accepts a predicate which we can pass to the Linq implementation. &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; TEntity Find(&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;Expression&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;Func&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity, &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;gt;&amp;gt; predicate) {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; Linq.Where(predicate).FirstOrDefault();
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;There&amp;#39;s one problem with this approach however. As you can see in the Linq property, we are passing in the current generic type of our repository. This means that our entity name mappings are not being used, and even worse than that, NHibernate will throw an error informing us that the type in question could not be found. This is to be expected, recall that we are overriding the implicit entity name, which defaults to the mapped types name, with our own. &lt;/p&gt;
&lt;p&gt;The current implementation of NHibernate.Linq is based on ICriteria, and ISession.CreateCriteria has an overload that will accept an entity name, just like all the other ISession methods. Because the entity name (or the Type) of an ICriteria can not be modified after it is created, we must control the creation of the ICriteria ourselves. The way I chose to do it was by adding a function callback to the QueryOptions object in NHibernate.Linq. This callback accepts the Linq ISession and the current expression tree and returns a new ICriteria. This lets us quickly create a delegate that will call Session.CreateCriteria(string) &lt;/p&gt;
&lt;pre&gt;	&lt;span style="font-size:x-small;"&gt;
	&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;protected&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#2b91af;"&gt;INHibernateQueryable&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;lt;TEntity&amp;gt; Linq {
		&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; {
			&lt;/span&gt;&lt;span style="font-size:x-small;color:#008000;"&gt;//create the new ICriteria with the current entity name, as opposed to the entity type&lt;/span&gt;
			&lt;span style="font-size:x-small;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; queryable = Session.Linq&amp;lt;TEntity&amp;gt;();
			queryable.QueryOptions.CreateCriteraFunction = (s, e) =&amp;gt; s.CreateCriteria(GetEntityName());&lt;/span&gt;
			
			&lt;span style="font-size:x-small;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:x-small;"&gt; queryable;
		}
	}
	&lt;/span&gt;
	&lt;/pre&gt;
&lt;p&gt;Because we are already using this property in our Find() method above, no changes are necessary, it will pick up this functionality automatically. &lt;/p&gt;
&lt;p&gt;The changes to the NHibernate.Linq assembly are quite modest, I have included a diff in this article which you can apply to r.803 to enable this feature. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Some Gotchas&lt;/h2&gt;
&lt;p&gt;As I mentioned previously, NHibernate specifies an entity name for all entities, whether you specify one or not. If you don&amp;#39;t, the entity name is taken from the full name of the mapped type. This means that NHibernate (currently) has no way of telling whether the entity name was explicitly specified, or taken from the class. This has an interesting side effect, which I alluded to at the start of the article, and answers the question of why you cant use a &amp;#39;default&amp;#39; mapping with no explicit entity name. &lt;/p&gt;
&lt;p&gt;The internals of NHibernate still have some assumptions that an entity name will be a full type name, including namespaces. The way things are currently implemented, it will examine the entity name to see if it contains a period (&amp;#39;.&amp;#39;), and if it does, it is assumed to be a class name, if it does not, it is assumed to be a user specified entity name. This means that all the existing code that expects an entity to be identified by its type will continue to work, and therefore, this behaviour is not likely to change any time soon. &lt;/p&gt;
&lt;p&gt;So why is that important? Well, if you provide an entity name that contains a period, some sections of NHibernate (specifically &lt;a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Impl/SessionFactoryImpl.cs"&gt;SessionFactoryImpl.GetImplementors(string)&lt;/a&gt;) will assume it to be a type name and will start looking for all the mappings related to the persistent class. The upshot of this is that it will eventually find all of the mappings, regardless of their entity names that we defined, and use them both. &lt;/p&gt;
&lt;p&gt;I first noticed this when I was getting an unexpected number of results in my tests, sometimes twice as many as I was expecting. The NHibernate logs showed that there were two queries being issued, one for each of the mappings, (table and view). It turned out to be the entity names I was using: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			The view specific mapping used the default (that us, unspecified) entity name: &amp;quot;My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			The table specific mapping used a mangled type name: &amp;quot;[AllTenants] My.Namespace.TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, both of these entity names contain periods. This caused NHibernate to use both sets of mappings when performing any operation, regardless of the entity name I specified, hence the two queries being issued. The solution is to specify an explicit entity name for both mappings which contains no periods: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
			View specific: &amp;quot;My_Namspace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;li&gt;
			Table specific: &amp;quot;[AllTenants] My_Namespace_TypeName&amp;quot;
		&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One downside of this approach, is that you must use the entity name in all references to the persistent class, i.e. in many-to-one and one-to-many elements. If you don&amp;#39;t, you will get an error when you try to create the session factory complaining about a reference to an unmapped type. Remember that we are overriding the default entity name behavior with our own, so NHibernate can no longer identify an entity using its type. In my particular case, I am generating the mappings dynamically, so this extra check is not a big problem, but if you are mapping by hand or using Fluent NHibernate, its something to be aware of. &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I&amp;#39;ve talked about mapping a single class to multiple tables (or views) using the new entity name feature in the upcoming version of NHibernate. I&amp;#39;ve also provided some suggestions on working with entity specific mappings, as well as some of the things you need to be aware of when using them. Finally, I&amp;#39;ve shown how Linq to NHibernate can be adapted for use with entity names. &lt;/p&gt;
&lt;p&gt;The example given here, of tenant specific filtering, is perhaps not the best way to get the job done. Something more along the lines of &lt;a href="/wikis/howtonh/contextual-data-using-nhibernate-filters.aspx"&gt;this article&lt;/a&gt; may be more appropriate for a greenfield application. But if you find yourself dealing with a legacy database that exhibits this requirement, the new features in NHibernate 2.1 will allow you to do so with little or no impact on your domain architecture. &lt;/p&gt;
&lt;h2&gt;Patch for NHibernate.Linq r.803&lt;/h2&gt;
&lt;pre&gt;Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs (revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/Visitors/NHibernateQueryTranslator.cs (working copy)
@@ -39,7 +39,12 @@
{
if (rootCriteria == null)
{
- rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+ if (options.CreateCriteraFunction != null) {
+ rootCriteria = options.CreateCriteraFunction(session, expr);
+ }
+ else {
+ rootCriteria = session.CreateCriteria(expr.ElementType, expr.Alias);
+ }
options.Execute(rootCriteria);
}
return expr;
Index: trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs
===================================================================
--- trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs (revision 803)
+++ trunk/src/NHibernate.Linq/src/NHibernate.Linq/QueryOptions.cs (working copy)
@@ -1,4 +1,6 @@
using System;
+using NHibernate.Criterion;
+using NHibernate.Linq.Expressions;

namespace NHibernate.Linq
{
@@ -9,6 +11,8 @@
{
private Action&amp;lt;ICriteria&amp;gt; action;

+ public Func&amp;lt;ISession, QuerySourceExpression, ICriteria&amp;gt; CreateCriteraFunction;
+
public QueryOptions()
{
this.action = delegate { };
	&lt;/pre&gt;
&lt;/div&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:01:26 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:50</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:01:26 p.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Audit&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt;&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest changes etc.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire&amp;nbsp;these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference&amp;nbsp;to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary domain.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping&amp;nbsp;file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/33.aspx</link><pubDate>Wed, 13 Jul 2011 10:56:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:512</guid><dc:creator>John Davidson</dc:creator><description>Revision 33 posted to How to by John Davidson on 13/07/2011 07:56:41 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest &lt;a href="http://www.steelecommerce.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Compare&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Telephone&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Service&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;changes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;etc&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire&amp;nbsp;these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference&amp;nbsp;to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary domain&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.ihs.com.tr/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table cellspacing="0" border="0" rules="NONE" frame="VOID"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width="358" height="19" align="LEFT"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;a href="http://childcarecenter.us/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping&amp;nbsp; &lt;a href="http://www.idevicenews.org/more-ipad-3-rumors/221723/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;iPad&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;3&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;rumors&lt;/span&gt;&amp;nbsp;file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/32.aspx</link><pubDate>Wed, 13 Jul 2011 06:13:14 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:499</guid><dc:creator>iPad 3 rumors</dc:creator><description>Revision 32 posted to How to by iPad 3 rumors on 13/07/2011 03:13:14 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest &lt;a href="http://www.steelecommerce.com/"&gt;Compare Telephone Service&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.steelecommerce.com/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table frame="VOID" rules="NONE" border="0" cellspacing="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="73"&gt;&lt;span style="font-family:Arial;color:#ff0000;font-size:xx-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire&amp;nbsp;these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference&amp;nbsp;to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary &lt;a href="http://www.ihs.com.tr/"&gt;domain &lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.ihs.com.tr/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table frame="VOID" rules="NONE" border="0" cellspacing="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="358"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;a href="http://childcarecenter.us/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping&amp;nbsp; &lt;a href="http://www.idevicenews.org/more-ipad-3-rumors/221723/"&gt;&lt;span style="background: SpringGreen;"&gt;iPad&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;rumors&lt;/span&gt;&lt;/a&gt; file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/31.aspx</link><pubDate>Wed, 13 Jul 2011 04:15:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:498</guid><dc:creator>domain</dc:creator><description>Revision 31 posted to How to by domain on 13/07/2011 01:15:56 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest &lt;a href="http://www.steelecommerce.com/"&gt;Compare Telephone Service&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.steelecommerce.com/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table frame="VOID" rules="NONE" border="0" cellspacing="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="73"&gt;&lt;span style="font-family:Arial;color:#ff0000;font-size:xx-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire&amp;nbsp;these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference&amp;nbsp;to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary &lt;span style="text-decoration: line-through; color: red;"&gt;key&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;a href="http://www.ihs.com.tr/"&gt;&lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.ihs.com.tr/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table frame="VOID" rules="NONE" border="0" cellspacing="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="358"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;a href="http://childcarecenter.us/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/30.aspx</link><pubDate>Tue, 12 Jul 2011 17:48:11 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:497</guid><dc:creator>Compare Telephone Service</dc:creator><description>Revision 30 posted to How to by Compare Telephone Service on 12/07/2011 02:48:11 p.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest &lt;span style="text-decoration: line-through; color: red;"&gt;change&lt;/span&gt;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;etc&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;a href="http://www.steelecommerce.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Compare&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Telephone&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Service&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.steelecommerce.com/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table frame="VOID" rules="NONE" border="0" cellspacing="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="73"&gt;&lt;span style="font-family:Arial;color:#ff0000;font-size:xx-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire&amp;nbsp;these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference&amp;nbsp;to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/29.aspx</link><pubDate>Mon, 11 Jul 2011 10:50:23 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:496</guid><dc:creator>John Davidson</dc:creator><description>Revision 29 posted to How to by John Davidson on 11/07/2011 07:50:23 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate&lt;a href="http://www.yiptel.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hosted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;voip&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest change&lt;a href="http://www.bhmpc.com/denial-management/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Health&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Insurance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Claim&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Denials&lt;/span&gt;&amp;nbsp;etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire&lt;a href="http://webstudio.web.tr/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;web&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sitesi&lt;/span&gt;&amp;nbsp;these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference&lt;a href="http://www.geniuschargers.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;car&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;battery&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;chargers&lt;/span&gt;&amp;nbsp;to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/28.aspx</link><pubDate>Mon, 11 Jul 2011 07:45:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:495</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 28 posted to How to by Scott Findlater on 11/07/2011 04:45:25 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate &lt;a href="http://www.yiptel.com/"&gt;hosted voip&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest change &lt;a href="http://www.bhmpc.com/denial-management/"&gt;Health Insurance Claim Denials&lt;/a&gt; etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire &lt;a href="http://webstudio.web.tr/"&gt;web sitesi&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://webstudio.web.tr/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table style="height:21px;" frame="VOID" rules="NONE" border="0" cellspacing="0" width="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="358"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;a href="http://childcarecenter.us/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp; these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference &lt;a href="http://www.geniuschargers.com/"&gt;car battery chargers&lt;/a&gt; to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/27.aspx</link><pubDate>Mon, 11 Jul 2011 07:26:33 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:494</guid><dc:creator>hosted voip</dc:creator><description>Revision 27 posted to How to by hosted voip on 11/07/2011 04:26:33 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate &lt;a href="http://www.yiptel.com/"&gt;&lt;span style="background: SpringGreen;"&gt;hosted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;voip&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest change &lt;a href="http://www.bhmpc.com/denial-management/"&gt;Health Insurance Claim Denials&lt;/a&gt; etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire &lt;a href="http://webstudio.web.tr/"&gt;web sitesi&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://webstudio.web.tr/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table style="height:21px;" frame="VOID" rules="NONE" border="0" cellspacing="0" width="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="358"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;a href="http://childcarecenter.us/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp; these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference &lt;a href="http://www.geniuschargers.com/"&gt;car battery chargers&lt;/a&gt; to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/26.aspx</link><pubDate>Mon, 11 Jul 2011 05:05:31 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:493</guid><dc:creator>web sitesi</dc:creator><description>Revision 26 posted to How to by web sitesi on 11/07/2011 02:05:31 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest change &lt;a href="http://www.bhmpc.com/denial-management/"&gt;Health Insurance Claim Denials&lt;/a&gt; etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire &lt;a href="http://webstudio.web.tr/"&gt;web sitesi&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://webstudio.web.tr/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table style="height:21px;" frame="VOID" rules="NONE" border="0" cellspacing="0" width="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="358"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;a href="http://childcarecenter.us/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp; these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference &lt;a href="http://www.geniuschargers.com/"&gt;car battery chargers&lt;/a&gt; to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/25.aspx</link><pubDate>Mon, 11 Jul 2011 05:04:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:492</guid><dc:creator>web sitesi</dc:creator><description>Revision 25 posted to How to by web sitesi on 11/07/2011 02:04:56 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest change &lt;a href="http://www.bhmpc.com/denial-management/"&gt;Health Insurance Claim Denials&lt;/a&gt; etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://webstudio.web.tr/"&gt;&lt;span style="background: SpringGreen;"&gt;web&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sitesi&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;table style="height:21px;" frame="VOID" rules="NONE" border="0" cellspacing="0" width="1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="LEFT" height="19" width="358"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;a href="http://childcarecenter.us/"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp; these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference &lt;a href="http://www.geniuschargers.com/"&gt;car battery chargers&lt;/a&gt; to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/24.aspx</link><pubDate>Fri, 08 Jul 2011 15:26:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:491</guid><dc:creator>Health Insurance Claim Denials</dc:creator><description>Revision 24 posted to How to by Health Insurance Claim Denials on 08/07/2011 12:26:53 p.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track&amp;nbsp; latest change &lt;a href="http://www.bhmpc.com/denial-management/"&gt;&lt;span style="background: SpringGreen;"&gt;Health&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Insurance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Claim&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Denials&lt;/span&gt;&lt;/a&gt; etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference &lt;a href="http://www.geniuschargers.com/"&gt;car battery chargers&lt;/a&gt; to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/23.aspx</link><pubDate>Tue, 05 Jul 2011 16:11:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:489</guid><dc:creator>car battery chargers</dc:creator><description>Revision 23 posted to How to by car battery chargers on 05/07/2011 01:11:22 p.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track latest change&amp;nbsp;etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference &lt;a href="http://www.geniuschargers.com/"&gt;&lt;span style="background: SpringGreen;"&gt;car&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;battery&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;chargers&lt;/span&gt;&lt;/a&gt; to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/22.aspx</link><pubDate>Tue, 05 Jul 2011 10:49:14 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:484</guid><dc:creator>John Davidson</dc:creator><description>Revision 22 posted to How to by John Davidson on 05/07/2011 07:49:14 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp&lt;a href="http://www.greeksubtitles.info/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Greek&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Subs&lt;/span&gt;. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track latest change&amp;nbsp;etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single&lt;a href="http://www.potenzmittel-scout.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Potenzmittel&lt;/span&gt;&amp;nbsp;table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps&lt;a href="http://www.saskatooncomputerrepair.ca/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Computer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Repair&lt;/span&gt;&amp;nbsp;the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/21.aspx</link><pubDate>Tue, 05 Jul 2011 04:45:31 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:483</guid><dc:creator>Greek Subs</dc:creator><description>Revision 21 posted to How to by Greek Subs on 05/07/2011 01:45:31 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp &lt;a href="http://www.greeksubtitles.info/"&gt;&lt;span style="background: SpringGreen;"&gt;Greek&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Subs&lt;/span&gt;&lt;/a&gt;. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track latest change&amp;nbsp;etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single &lt;a href="http://www.potenzmittel-scout.com/"&gt;Potenzmittel&lt;/a&gt; table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps &lt;a href="http://www.saskatooncomputerrepair.ca/"&gt;Computer Repair&lt;/a&gt; the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/20.aspx</link><pubDate>Tue, 05 Jul 2011 03:29:19 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:482</guid><dc:creator>Computer Repair</dc:creator><description>Revision 20 posted to How to by Computer Repair on 05/07/2011 12:29:19 a.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track latest change&amp;nbsp;etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single &lt;a href="http://www.potenzmittel-scout.com/"&gt;Potenzmittel&lt;/a&gt; table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps &lt;a href="http://www.saskatooncomputerrepair.ca/"&gt;&lt;span style="background: SpringGreen;"&gt;Computer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Repair&lt;/span&gt;&lt;/a&gt; the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating an Audit Log using NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events/revision/19.aspx</link><pubDate>Tue, 05 Jul 2011 02:14:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:481</guid><dc:creator>Potenzmittel</dc:creator><description>Revision 19 posted to How to by Potenzmittel on 04/07/2011 11:14:53 p.m.&lt;br /&gt;
&lt;h2&gt;Creating an Audit Log using NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events&lt;/div&gt;

&lt;h2&gt;Scenario&lt;/h2&gt;
&lt;p&gt;You want to create an audit table so that changes to business entities are tracked with a timestamp. You want this do be done automatically by NHibernate.&lt;/p&gt;
&lt;h2&gt;Options&lt;/h2&gt;
&lt;p&gt;There are a number of ways of doing this, using IInterceptor or the NHibernate 2.0 Event model. As the event model is fairly new, there isn&amp;#39;t a lot of information or examples about how to use it therefore most examples deal with IInterceptor.The Audit logging itself can be recorded via multiple tables per class, single table to cover all classes, track all changes, track latest change&amp;nbsp;etc.&lt;/p&gt;
&lt;h2&gt;Solution&lt;/h2&gt;
&lt;p&gt;This solution uses the Event model, a single &lt;a href="http://www.potenzmittel-scout.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Potenzmittel&lt;/span&gt;&lt;/a&gt; table for all classes and track latest change only. It also depends on the database key being a GUID, not an integer. When a class is deleted, the audit information is also removed. This might not be appropriate for your scenario.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;The solution operates within a repository pattern (nielson, Domain Driven Design) design where there is a single&amp;nbsp;repository wrapping the NHibernate Session. This repository exposes an Interface that is defined in the business layer that the NHibernate Repository implements. Therefore the repository depends on the domain, not the domain depends on the repository. An IoC product (Spring.NET, Unity etc.) is used to wire these&amp;nbsp;together.&lt;/p&gt;
&lt;p&gt;The Domain has no reference to anything to do with NHibernate but, to ease development; each business entity inherits from the abstract Entity class and implements the IEntity interface. It is the Entity class that tracks the audit information for the business class that inherits from it.&lt;/p&gt;
&lt;p&gt;Entity Id&amp;#39;s are GUID&amp;#39;s as opposed to integers. The entity Id maps to the database primary key.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;The abstract Entity class is as follows&lt;/p&gt;
&lt;p&gt;[Serializable]&lt;br /&gt;public abstract class Entity : IEntity&lt;br /&gt;{&lt;br /&gt;private const string UnknownUser = &amp;quot;Unknown&amp;quot;; &lt;/p&gt;
&lt;p&gt;private Guid _id;&lt;br /&gt;private byte[] _version;&lt;br /&gt;private DateTime _createdTimestamp;&lt;br /&gt;private string _createdBy;&lt;br /&gt;private DateTime _updatedTimestamp;&lt;br /&gt;private string _updatedBy; &lt;/p&gt;
&lt;p&gt;[Snip: Property declarations removed for brevity]&lt;/p&gt;
&lt;p&gt;public Entity()&lt;br /&gt;{&lt;br /&gt;_id = Guid.Empty;&lt;br /&gt;_version = new byte[8];&lt;br /&gt;_createdBy = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? UnknownUser : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;_createdTimestamp = DateTime.Now;&lt;br /&gt;_entityName = this.GetType().FullName;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Each business class extends this however is appropriate.&lt;/p&gt;
&lt;h3&gt;Mappings&lt;/h3&gt;
&lt;p&gt;An example mapping file for a class that extends the entity class might be:&lt;/p&gt;
&lt;p&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; namespace=&amp;quot;Sample.Domain&amp;quot; assembly=&amp;quot;Sample.Domain&amp;quot; default-access=&amp;quot;field.camelcase-underscore&amp;quot; default-lazy=&amp;quot;true&amp;quot;&amp;gt; &lt;br /&gt;&amp;lt;class name=&amp;quot;Task&amp;quot; proxy=&amp;quot;Sample.Domain.Interfaces.ITask, Sample.Domain.Interfaces&amp;quot; table=&amp;quot;Task&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;id name=&amp;quot;Id&amp;quot; column=&amp;quot;Id&amp;quot; unsaved-value=&amp;quot;00000000-0000-0000-0000-000000000000&amp;quot; access=&amp;quot;property&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;generator class=&amp;quot;guid.comb&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;lt;version name=&amp;quot;Version&amp;quot; column=&amp;quot;Version&amp;quot; type=&amp;quot;binary&amp;quot; unsaved-value=&amp;quot;null&amp;quot; generated=&amp;quot;always&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Lookup&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;50&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;Description&amp;quot; type=&amp;quot;string&amp;quot; length=&amp;quot;255&amp;quot; not-null=&amp;quot;true&amp;quot; /&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;lt;join table=&amp;quot;Audit&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;key column=&amp;quot;EntityId&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;EntityName&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;CreatedTimestamp&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedBy&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;property name=&amp;quot;UpdatedTimestamp&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/p&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
&lt;p&gt;From the mapping&amp;nbsp;we can see that the Task class uses an Interface (ITask) for its proxy generation (though virtual methods are just as acceptable) but more importantly that the Task class is populated from a join between the Audit table and the Task table. The join column is the Entity ID which, like the Id in the task table is a GUID. As GUID&amp;#39;s are almost guaranteed to be unique there is very little likelihood of the being a key collision between the Task table and (e.g.) a Group table each storing their ID in the same column. Unfortunatley this is not the case for a HiLo key generation mechanism.&lt;/p&gt;
&lt;p&gt;Therefore this Join element is telling NHibernate&amp;nbsp;to do a SQL join on the Id of the primary table (Task) with the Entity ID of the Audit table. In effect, its the same as multi table inheritence but without the inheritence.&lt;/p&gt;
&lt;p&gt;The power of this is that Nhibernate automatically keeps the two tables in line... an add to Task results in two SQL inserts (Task and Audit), a delete of Task results in two deletes etc. If you wish to retain audit information, but not the business data, in the case of a delete then this solution is not for you, though you could consider the &amp;quot;pre-delete&amp;quot; event to deal with retaining the audit information somehow.&lt;/p&gt;
&lt;h3&gt;Wiring into the NHibernate Event model&lt;/h3&gt;
&lt;p&gt;The NHibernate 2.0 Event model can be implemented a number of ways, inherit from a base class or implement an interface. This solution uses an interface as it allows a single listener class to deal with both updates and inserts.&lt;/p&gt;
&lt;p&gt;The best events to use for Auditing in this scenario are &amp;quot;pre-update&amp;quot; and &amp;quot;pre-insert&amp;quot;. First we write a listener class that implements two interfaces IPreUpdateEventListener and IPreInsertEventListener. This class needs to ensure that the &amp;quot;state&amp;quot; of the object to be written is updated prior to NHibernate writing out the class information. Unfortunately this state information is held in a string array and cannot be accessed in a TypeSafe manner.&lt;/p&gt;
&lt;p&gt;internal class AuditEventListener : IPreUpdateEventListener, IPreInsertEventListener&lt;br /&gt;{&lt;br /&gt;public bool OnPreUpdate(PreUpdateEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false; &lt;br /&gt;}&lt;br /&gt;public bool OnPreInsert(PreInsertEvent e)&lt;br /&gt;{&lt;br /&gt;UpdateAuditTrail(e.State, e.Persister.PropertyNames, (IEntity)e.Entity);&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;private void UpdateAuditTrail(object[] state, string[] names, IEntity entity)&lt;br /&gt;{&lt;br /&gt;var idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedBy&amp;quot;);&lt;br /&gt;state[idx] = string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ? &amp;quot;Unknown&amp;quot; : Thread.CurrentPrincipal.Identity.Name;&lt;br /&gt;entity.UpdatedBy = state[idx].ToString();&lt;br /&gt;idx = Array.FindIndex(names, n =&amp;gt; n == &amp;quot;UpdatedTimestamp&amp;quot;);&lt;br /&gt;DateTime now = DateTime.Now;&lt;br /&gt;state[idx] = now;&lt;br /&gt;entity.UpdatedTimestamp = now;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t ask me why we return false in the implemented methods.... I&amp;#39;m not sure yet, but it works :)&lt;/p&gt;
&lt;p&gt;So now when an object is written&amp;nbsp;(the presumption is that all objects written implement the business IEntity interface) Nhibernate will run the listener, find the audit properties in the current NHibernate State and update them.To ensure the object itself also has the latest values, we update the entity instance as well.&lt;/p&gt;
&lt;h3&gt;Hooking NHibernate into the Listener&lt;/h3&gt;
&lt;p&gt;This is the easy bit - add the following to the hibernate.cfg.xml file (within the SessionFactory element) and you&amp;#39;re done!&lt;/p&gt;
&lt;p&gt;&amp;lt;event type=&amp;quot;pre-update&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt; &lt;br /&gt;&amp;lt;event type=&amp;quot;pre-insert&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;listener class=&amp;quot;Sample.Repository.NHibernate.AuditEventListener, Sample.Repository.NHibernate&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/event&amp;gt;&lt;/p&gt;
&lt;h3&gt;A Side Note&lt;/h3&gt;
&lt;p&gt;This article works great, thanks Graham.&amp;nbsp; I just wanted to point out there is an NHibernate gotcha/ feature related to using the OnPre* events with inheritance which is &lt;a href="/wikis/howtonh/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners.aspx"&gt;documented further on this WIKI post. &lt;/a&gt;&lt;/p&gt;</description></item><item><title>Retrieve a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/0.aspx</link><pubDate>Tue, 23 Aug 2011 11:38:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:53</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 23/08/2011 08:38:17 a.m.&lt;br /&gt;
&lt;h2&gt;Retrieve a nested component with Criteria API&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Projections&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Criteria&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;API&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Component&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist.&lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-certificate.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Graduate&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;Certificate&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/diploma/undergraduate-diploma.html"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Undergraduate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/schools-majors/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Schools&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar);&lt;a href="http://www.mustuniversity.com/programs/diploma/graduate-diploma.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Graduate&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/phd-degree.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Phd&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar. i recently started automated forex trading and the results so far have 
been great, i highly recommend it to anyone!&lt;a rel="follow" href="http://www.mustuniversity.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt;
 &lt;a rel="follow" href="http://www.mustuniversity.com/programs/degree/bachelors-degree.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Bachelor&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Very&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;well&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;said&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;think&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;everyone&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;thinks&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;way&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;world&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;would&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;much&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;better&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;place.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;especially&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;my&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cash&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;out&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;refinance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;gets&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;approved&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt; &lt;a rel="follow" href="http://www.mustuniversity.com/must/distancelearning.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Distance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Learning&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a rel="follow" href="http://www.mustuniversity.com/must/affordableeducation.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Affordable&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a rel="follow" href="http://www.mustuniversity.com/schools-majors/education/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Education&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Retrieve a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/7.aspx</link><pubDate>Tue, 23 Aug 2011 11:30:36 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:511</guid><dc:creator>John Davidson</dc:creator><description>Revision 7 posted to How to by John Davidson on 23/08/2011 08:30:36 a.m.&lt;br /&gt;
&lt;h2&gt;Retrieve a nested component with Criteria API&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Projections, Criteria API, Component&lt;/div&gt;

&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task&lt;a href="http://www.assignmentexpert.com/math/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;math&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;help&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;probability&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist. &lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-certificate.html"&gt;Graduate
 Certificate&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/programs/diploma/undergraduate-diploma.html"&gt;
Undergraduate Diploma&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/schools-majors/"&gt;Online
Schools&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar); &lt;a href="http://www.mustuniversity.com/programs/diploma/graduate-diploma.html"&gt;Graduate
 Diploma&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/programs/degree/phd-degree.html"&gt;Phd
Degree&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar. i recently started automated forex trading and the results so far have 
been great, i highly recommend it to anyone! &lt;a rel="follow" href="http://www.mustuniversity.com/"&gt;Online Degree&lt;/a&gt; AND
 &lt;a rel="follow" href="http://www.mustuniversity.com/programs/degree/bachelors-degree.html"&gt;Bachelor Degree&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Very well said, I think that if everyone thinks this way that the world 
would be a much better place.. especially if my cash out refinance gets 
approved! &lt;a rel="follow" href="http://www.mustuniversity.com/must/distancelearning.asp"&gt;Distance Learning&lt;/a&gt; AND &lt;a rel="follow" href="http://www.mustuniversity.com/must/affordableeducation.asp"&gt;Affordable Degree&lt;/a&gt; AND &lt;a rel="follow" href="http://www.mustuniversity.com/schools-majors/education/"&gt;Education Degree&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Retrieve a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/6.aspx</link><pubDate>Tue, 23 Aug 2011 08:57:14 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:510</guid><dc:creator>piter</dc:creator><description>Revision 6 posted to How to by piter on 23/08/2011 05:57:14 a.m.&lt;br /&gt;
&lt;h2&gt;Retrieve a nested component with Criteria API&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Projections, Criteria API, Component&lt;/div&gt;

&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task &lt;a href="http://www.assignmentexpert.com/math/"&gt;&lt;span style="background: SpringGreen;"&gt;math&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;help&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;probability&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist. &lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-certificate.html"&gt;Graduate
 Certificate&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/programs/diploma/undergraduate-diploma.html"&gt;
Undergraduate Diploma&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/schools-majors/"&gt;Online
Schools&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar); &lt;a href="http://www.mustuniversity.com/programs/diploma/graduate-diploma.html"&gt;Graduate
 Diploma&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/programs/degree/phd-degree.html"&gt;Phd
Degree&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar. i recently started automated forex trading and the results so far have 
been great, i highly recommend it to anyone! &lt;a href="http://www.mustuniversity.com/" rel="follow"&gt;Online Degree&lt;/a&gt; AND
 &lt;a href="http://www.mustuniversity.com/programs/degree/bachelors-degree.html" rel="follow"&gt;Bachelor Degree&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Very well said, I think that if everyone thinks this way that the world 
would be a much better place.. especially if my cash out refinance gets 
approved! &lt;a href="http://www.mustuniversity.com/must/distancelearning.asp" rel="follow"&gt;Distance Learning&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/must/affordableeducation.asp" rel="follow"&gt;Affordable Degree&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/schools-majors/education/" rel="follow"&gt;Education Degree&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Retrieve a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/5.aspx</link><pubDate>Wed, 30 Mar 2011 11:04:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:509</guid><dc:creator>markweee</dc:creator><description>Revision 5 posted to How to by markweee on 30/03/2011 08:04:24 a.m.&lt;br /&gt;
&lt;h2&gt;Retrieve a nested component with Criteria API&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Projections, Criteria API, Component&lt;/div&gt;

&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist. &lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-certificate.html"&gt;&lt;span style="background: SpringGreen;"&gt;Graduate&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Certificate&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/diploma/undergraduate-diploma.html"&gt;
&lt;span style="background: SpringGreen;"&gt;Undergraduate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/schools-majors/"&gt;&lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Schools&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar); &lt;a href="http://www.mustuniversity.com/programs/diploma/graduate-diploma.html"&gt;&lt;span style="background: SpringGreen;"&gt;Graduate&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Diploma&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/phd-degree.html"&gt;&lt;span style="background: SpringGreen;"&gt;Phd&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar. i recently started automated forex trading and the results so far have 
been great, i highly recommend it to anyone! &lt;a href="http://www.mustuniversity.com/" rel="follow"&gt;Online Degree&lt;/a&gt; AND
 &lt;a href="http://www.mustuniversity.com/programs/degree/bachelors-degree.html" rel="follow"&gt;Bachelor Degree&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Very well said, I think that if everyone thinks this way that the world 
would be a much better place.. especially if my cash out refinance gets 
approved! &lt;a href="http://www.mustuniversity.com/must/distancelearning.asp" rel="follow"&gt;Distance Learning&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/must/affordableeducation.asp" rel="follow"&gt;Affordable Degree&lt;/a&gt; AND &lt;a href="http://www.mustuniversity.com/schools-majors/education/" rel="follow"&gt;Education Degree&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Retrieve a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/4.aspx</link><pubDate>Wed, 30 Mar 2011 10:50:11 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:368</guid><dc:creator>markweee</dc:creator><description>Revision 4 posted to How to by markweee on 30/03/2011 07:50:11 a.m.&lt;br /&gt;
&lt;h2&gt;Retrieve a nested component with Criteria API&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Projections, Criteria API, Component&lt;/div&gt;

&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist.&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar. i recently started automated forex trading and the results so far have 
been great, i highly recommend it to anyone! &lt;a href="http://www.mustuniversity.com/" rel="follow"&gt;Online Degree&lt;/a&gt; AND
 &lt;a href="http://www.mustuniversity.com/programs/degree/bachelors-degree.html" rel="follow"&gt;Bachelor Degree&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Very&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;well&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;said&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;think&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;everyone&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thinks&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;way&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;world&lt;/span&gt; 
&lt;span style="background: SpringGreen;"&gt;would&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;much&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;better&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;place.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;especially&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;my&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cash&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;out&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;refinance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;gets&lt;/span&gt; 
&lt;span style="background: SpringGreen;"&gt;approved&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/must/distancelearning.asp" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Distance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Learning&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/must/affordableeducation.asp" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Affordable&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/schools-majors/education/" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Education&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Retrieve a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/3.aspx</link><pubDate>Wed, 30 Mar 2011 10:49:28 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:366</guid><dc:creator>markweee</dc:creator><description>Revision 3 posted to How to by markweee on 30/03/2011 07:49:28 a.m.&lt;br /&gt;
&lt;h2&gt;Retrieve a nested component with Criteria API&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Projections, Criteria API, Component&lt;/div&gt;

&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist.&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar. &lt;span style="background: SpringGreen;"&gt;i&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;started&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;automated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;forex&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;results&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;so&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;far&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; 
&lt;span style="background: SpringGreen;"&gt;been&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;great&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;i&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;highly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recommend&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;anyone&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt;
 &lt;a href="http://www.mustuniversity.com/programs/degree/bachelors-degree.html" rel="follow"&gt;&lt;span style="background: SpringGreen;"&gt;Bachelor&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Retrieve a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/2.aspx</link><pubDate>Tue, 31 Mar 2009 22:50:34 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:365</guid><dc:creator>Kenneth Siewers Møller</dc:creator><description>Revision 2 posted to How to by Kenneth Siewers Møller on 31/03/2009 07:50:34 p.m.&lt;br /&gt;
&lt;h2&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Retrieving&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Retrieve&lt;/span&gt; a nested component with Criteria API&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Projections, Criteria API, Component&lt;/div&gt;

&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist.&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar.&lt;/p&gt;</description></item><item><title>Retrieving a nested component with Criteria API</title><link>http://nhforge.org/wikis/howtonh/retrieve-a-nested-component-with-criteria-api/revision/1.aspx</link><pubDate>Wed, 11 Mar 2009 20:32:23 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:254</guid><dc:creator>Kenneth Siewers Møller</dc:creator><description>Revision 1 posted to How to by Kenneth Siewers Møller on 11/03/2009 05:32:23 p.m.&lt;br /&gt;
&lt;p&gt;Sometimes it may be necessary to retrieve a nested component type of a class. NHibernate does not provide any direct solution for this, and the most obvious aproach using a projection does not work.&lt;/p&gt;
&lt;p&gt;Using regular HQL it is possible to retrieve a component, but in more advanced situations it can be a pain concatenating HQL strings to perform a task.&lt;/p&gt;
&lt;p&gt;What we will be accomplishing is this:&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s assume we have an entity called Foo. Foo has a nested component called Bar. Bar is mapped using &amp;lt;component&amp;gt; in the mapping file for Foo.&lt;/p&gt;
&lt;p&gt;To return only Bar from Foo with id 12 we&amp;#39;d write something similar to this using HQL:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooById(&lt;span style="color:blue;"&gt;int&lt;/span&gt; id)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(&lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from Foo f where f.Id = :id&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(&lt;span style="color:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, id);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above code is pretty simple, and is pretty useful. However, when things becomes a bit more complex it can be really ugly, like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; hql = &lt;span style="color:#a31515;"&gt;&amp;quot;select f.Bar from foo&amp;quot;&lt;/span&gt;; &lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; parameters = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:blue;"&gt;object&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where f.Baz = :bazValue&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;bazValue&amp;quot;&lt;/span&gt;, bazValue);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Id = :fooId&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;fooId&amp;quot;&lt;/span&gt;, fooId.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!hql.Contains(&lt;span style="color:#a31515;"&gt;&amp;quot;where&amp;quot;&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; where&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; and&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; hql += &lt;span style="color:#a31515;"&gt;&amp;quot; f.Bar.TheProperty = :mustHaveProperty&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; parameters.Add(&lt;span style="color:#a31515;"&gt;&amp;quot;mustHaveProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; query = CurrentSession.CreateQuery(hql);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; parameter &lt;span style="color:blue;"&gt;in&lt;/span&gt; parameters)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; query.SetParameter(parameter.Key, parameter.Value);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; query.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the above, not so unrealistic, example makes it pretty difficult to work with HQL in a very dynamic way. Fortunately the Criteria API is very flexible in this regards, but is missing a couple of features only supported in HQL. In the real world you&amp;#39;d probably come up with some smart mutators for the string etc. but this was just to prove a point.&lt;/p&gt;
&lt;p&gt;The example above could be written like the following using the Criteria API (although it won&amp;#39;t work, it serves as an example to support the actual solution):&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt; GetBarFromFooBySomeProperties(&lt;span style="color:blue;"&gt;string&lt;/span&gt; bazValue, &lt;span style="color:blue;"&gt;int&lt;/span&gt;? fooId, &lt;span style="color:blue;"&gt;bool&lt;/span&gt;? mustHaveProperty)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;var&lt;/span&gt; criteria = CurrentSession.CreateCriteria(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Foo&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;f&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (!&lt;span style="color:blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(bazValue))&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Baz&amp;quot;&lt;/span&gt;, bazValue));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (fooId.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Id&amp;quot;&lt;/span&gt;, fooId.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;if&lt;/span&gt; (mustHaveProperty.HasValue)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.Add(&lt;span style="color:#2b91af;"&gt;Restrictions&lt;/span&gt;.Eq(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar.TheProperty&amp;quot;&lt;/span&gt;, mustHaveProperty.Value));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;f.Bar&amp;quot;&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; criteria.SetResultTransformer(&lt;span style="color:#2b91af;"&gt;Transformers&lt;/span&gt;.AliasToBean(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;)));&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; criteria.UniqueResult&amp;lt;&lt;span style="color:#2b91af;"&gt;Bar&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The example above will throw a runtime exception because the projection does not map to a single property. Of course this makes sense in the absolute most strict way as Bar is not a simple property. The point, however, is that the Criteria API makes it much easier to work with dynamic queries and it is very powerful.&lt;/p&gt;
&lt;p&gt;Since there is no built-in support (that I know of that is) for this type of projection, the only way is to manually do the mapping using a projectionlist.&lt;/p&gt;
&lt;p&gt;One way to do this would be to create a static class containing the projectionlist like this:&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;internal&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IProjection&lt;/span&gt; Bar&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.ProjectionList()&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.Property1&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;Property1&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.TheProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;TheProperty&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .Add(&lt;span style="color:#2b91af;"&gt;Projections&lt;/span&gt;.Property(&lt;span style="color:#a31515;"&gt;&amp;quot;Bar.AnotherProperty&amp;quot;&lt;/span&gt;), &lt;span style="color:#a31515;"&gt;&amp;quot;AnotherProperty&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With the projectionlist in place, it&amp;#39;s simply a question of using this in the example above like this:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:10pt;color:black;"&gt;
&lt;p style="margin:0px;"&gt;criteria.SetProjection(&lt;span style="color:#2b91af;"&gt;MyProjections&lt;/span&gt;.Bar);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we can return the mapped component class. Of course the projection is not safe in a deeper nesting, but this could be solved by making a projection factory or something similar.&lt;/p&gt;</description></item><item><title>A fully working skeleton for sexy Loquacious NH</title><link>http://nhforge.org/wikis/howtonh/a-fully-working-skeleton-for-sexy-loquacious-nh/revision/0.aspx</link><pubDate>Wed, 17 Aug 2011 22:28:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:94</guid><dc:creator>Scott Findlater</dc:creator><description>Current revision posted to How to by Scott Findlater on 17/08/2011 07:28:41 p.m.&lt;br /&gt;
&lt;h2&gt;A fully working skeleton for sexy Loquacious NH&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;After spending the last few months learning NHibernate, starting with hbm xml, to Fluent NHibernate, then Loquacious sexy mapping was released. &lt;/p&gt;
&lt;p&gt;Herewith is a solution, with tests and ioc, for a fully working skeleton demonstrating both explicit mapping by code and mapping by convention.&lt;/p&gt;
&lt;p&gt;Domain; details the problem domain ... a small Customer, Order, Product etc domain but with all the fun relationships!!&lt;/p&gt;
&lt;p&gt;Domain.NHByCode; is the mapping of the domain using the ModelMapper, with Explicit Mappings.  Sorry Fabio, I have been very conformist here with single mapping classes ;)&lt;/p&gt;
&lt;p&gt;Domain.NHByConvention; is the mapping of the same domain using the ConventionModelMapper.&lt;/p&gt;
&lt;p&gt;These are the key blog posts by Fabio;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;01 04 2011 - NHibernate 3.2 mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code.html&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;08 04 2011 - NHibernate 3.2: (part 2) mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-part-2-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-part-2-mapping-by-code.html&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;13 04 2011 - NHibernate 3.2: mapping by code conventions;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code_13.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code_13.html&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;16 07 2011 - NHibernate: playing with mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;17 07 2011 - NHibernate: playing with mapping by code (2);
&lt;a href="http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code_17.html"&gt;http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code_17.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Other resources;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.nhforge.org/doc/nh/en/index.html"&gt;http://www.nhforge.org/doc/nh/en/index.html&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="/wikis/howtonh/a-fully-working-skeleton-for-sexy-loquacious-nh/"&gt;http://nhforge.org/wikis/&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;A nice NH set vs bag answer &lt;a href="http://stackoverflow.com/questions/1916350/set-bag-and-list-set-in-nhibernate/1921727#1921727"&gt;http://stackoverflow.com/questions/1916350/set-bag-and-list-set-in-nhibernate/1921727#1921727&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Grab the code here - &lt;a href="https://github.com/ScottFindlater/Loquacious-NH"&gt;https://github.com/ScottFindlater/Loquacious-NH&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A fully working skeleton for sexy Loquacious NH</title><link>http://nhforge.org/wikis/howtonh/a-fully-working-skeleton-for-sexy-loquacious-nh/revision/2.aspx</link><pubDate>Wed, 17 Aug 2011 22:27:14 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:508</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 2 posted to How to by Scott Findlater on 17/08/2011 07:27:14 p.m.&lt;br /&gt;
&lt;h2&gt;A fully working skeleton for sexy Loquacious NH&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping by code&lt;/div&gt;

&lt;p&gt;After spending the last few months learning NHibernate, starting with hbm xml, to Fluent NHibernate, then Loquacious sexy mapping was released. &lt;/p&gt;
&lt;p&gt;Herewith is a solution, with tests and ioc, for a fully working skeleton demonstrating both explicit mapping by code and mapping by convention.&lt;/p&gt;
&lt;p&gt;Domain; details the problem domain ... a small Customer, Order, Product etc domain but with all the fun relationships!!&lt;/p&gt;
&lt;p&gt;Domain.NHByCode; is the mapping of the domain using the ModelMapper, with Explicit Mappings.  Sorry Fabio, I have been very conformist here with single mapping classes ;)&lt;/p&gt;
&lt;p&gt;Domain.NHByConvention; is the mapping of the same domain using the ConventionModelMapper.&lt;/p&gt;
&lt;p&gt;These are the key blog posts by Fabio;
01 04 2011 - NHibernate 3.2 mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code.html&lt;/a&gt;
08 04 2011 - NHibernate 3.2: (part 2) mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-part-2-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-part-2-mapping-by-code.html&lt;/a&gt;
13 04 2011 - NHibernate 3.2: mapping by code conventions;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code_13.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code_13.html&lt;/a&gt;
16 07 2011 - NHibernate: playing with mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html&lt;/a&gt;
17 07 2011 - NHibernate: playing with mapping by code (2);
&lt;a href="http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code_17.html"&gt;http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code_17.html&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;Other resources;
&lt;a href="http://www.nhforge.org/doc/nh/en/index.html"&gt;http://www.nhforge.org/doc/nh/en/index.html&lt;/a&gt;
&lt;a href="/wikis/howtonh/a-fully-working-skeleton-for-sexy-loquacious-nh/"&gt;http://nhforge.org/wikis/&lt;/a&gt;
A nice NH set vs bag answer &lt;a href="http://stackoverflow.com/questions/1916350/set-bag-and-list-set-in-nhibernate/1921727#1921727"&gt;http://stackoverflow.com/questions/1916350/set-bag-and-list-set-in-nhibernate/1921727#1921727&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Grab the code here - &lt;a href="https://github.com/ScottFindlater/Loquacious-NH"&gt;https://github.com/ScottFindlater/Loquacious-NH&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A fully working skeleton for sexy Loquacious NH</title><link>http://nhforge.org/wikis/howtonh/a-fully-working-skeleton-for-sexy-loquacious-nh/revision/1.aspx</link><pubDate>Wed, 17 Aug 2011 21:20:50 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:507</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 1 posted to How to by Scott Findlater on 17/08/2011 06:20:50 p.m.&lt;br /&gt;
&lt;pre&gt;After spending the last few months learning NHibernate, starting with hbm xml, to Fluent NHibernate, then Loquacious sexy mapping was released.  &lt;br /&gt;Herewith is a solution, with tests and ioc, for a fully working skeleton demonstrating both explicit mapping by code and mapping by convention.

Domain; details the problem domain ... a small Customer, Order, Product etc domain but with all the fun relationships!!

Domain.NHByCode; is the mapping of the domain using the ModelMapper, with Explicit Mappings.  Sorry Fabio, I have been very conformist here with single mapping classes ;)

Domain.NHByConvention; is the mapping of the same domain using the ConventionModelMapper.

These are the key blog posts by Fabio;
01 04 2011 - NHibernate 3.2 mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code.html&lt;/a&gt;

08 04 2011 - NHibernate 3.2: (part 2) mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-part-2-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-part-2-mapping-by-code.html&lt;/a&gt;

13 04 2011 - NHibernate 3.2: mapping by code conventions;
&lt;a href="http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code_13.html"&gt;http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code_13.html&lt;/a&gt;

16 07 2011 - NHibernate: playing with mapping by code;
&lt;a href="http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html"&gt;http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html&lt;/a&gt;

17 07 2011 - NHibernate: playing with mapping by code (2);
&lt;a href="http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code_17.html"&gt;http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code_17.html&lt;/a&gt;

Other resources;
&lt;a href="http://www.nhforge.org/doc/nh/en/index.html"&gt;http://www.nhforge.org/doc/nh/en/index.html&lt;/a&gt;
&lt;a&gt;http://nhforge.org/wikis/&lt;/a&gt;
A nice NH set vs bag answer &lt;a href="http://stackoverflow.com/questions/1916350/set-bag-and-list-set-in-nhibernate/1921727#1921727"&gt;http://stackoverflow.com/questions/1916350/set-bag-and-list-set-in-nhibernate/1921727#1921727&lt;/a&gt;

Grab the code here - &lt;a href="https://github.com/ScottFindlater/Loquacious-NH"&gt;https://github.com/ScottFindlater/Loquacious-NH&lt;/a&gt;&lt;/pre&gt;</description></item><item><title>.NET type generation for NHibernate mapping collections</title><link>http://nhforge.org/wikis/howtonh/net-type-generation-for-nhibernate-mapping-collections/revision/0.aspx</link><pubDate>Tue, 09 Aug 2011 09:01:00 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:93</guid><dc:creator>Igor Ignatov</dc:creator><description>Current revision posted to How to by Igor Ignatov on 09/08/2011 06:01:00 a.m.&lt;br /&gt;
&lt;p&gt;As our users might know, the Devart company has recently released a new version of &amp;nbsp;Entity Developer that supports NHibernate that is intended to provide users with a powerful and convenient &lt;a href="http://www.devart.com/entitydeveloper/nhibernate-designer.html"&gt;visual model designer for NHibernate&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When writing templates for generating entity classes for NHibernate models in &amp;nbsp;Devart Entity Developer for NHibernate, I tried to understand to which .NET types in generated entity classes NHibernate collection were mapped to and in which structure they were stored. I read quite a lot of online articles on the subject, but none provided me an exhaustive answer. After I cleared up this issue, I decided to describe this aspect in an article.&lt;/p&gt;
&lt;p&gt;So, there are the following types of collection mapping in NHibernate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Set;&lt;/li&gt;
&lt;li&gt;List;&lt;/li&gt;
&lt;li&gt;Bag;&lt;/li&gt;
&lt;li&gt;Idbag;&lt;/li&gt;
&lt;li&gt;Map;&lt;/li&gt;
&lt;li&gt;Array.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The Generic collection can be used in all type of NHibernate collection mapping, except for Array.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Set &lt;/strong&gt;maps to an Iesi.Collections.ISet. The interface is part of the Iesi.Collections assembly distributed with NHibernate. The field of the class, in which the collection is stored, is initialized with an object of the &amp;nbsp;Iesi.Collections.HashedSet type; it is also part of Iesi.Collections assembly distributed with NHibernate. If the collection is mapped as the generic one, &lt;strong&gt;Set&lt;/strong&gt; maps to an Iesi.Collections.Generic.&lt;i&gt;&lt;strong&gt;ISet&lt;/strong&gt;&lt;/i&gt;&amp;lt;T&amp;gt;, while the field is initialized with an object of the Iesi.Collections.Generic.&lt;i&gt;&lt;strong&gt;HashedSet&lt;/strong&gt;&lt;/i&gt;&amp;lt;T&amp;gt; type, where T is the details entity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;List&lt;/strong&gt;, &lt;strong&gt;Bag&lt;/strong&gt; and &lt;strong&gt;Idbag&lt;/strong&gt; map to System.Collections.IList. The field of the class, in which the collection is stored, is initialized with an object of the System.Collections.ArrayList type. When mapped as generic, the collection maps to &lt;i&gt;&lt;strong&gt;System.Collections.Generic.IList&lt;/strong&gt;&lt;/i&gt;&amp;lt;T&amp;gt;, while the field is initialized with an object of the &lt;i&gt;&lt;strong&gt;System.Collections.Generic.List&lt;/strong&gt;&lt;/i&gt;&amp;lt;T&amp;gt; type, where T is the details entity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Map&lt;/strong&gt; maps to System.Collections.IDictionary. The field of the class, in which the collection is stored, is initialized with an object of the System.Collections.&lt;i&gt;&lt;strong&gt;Hashtable&lt;/strong&gt;&lt;/i&gt; type. If the collection is mapped as the generic one, &lt;strong&gt;Map&lt;/strong&gt; maps to &amp;nbsp;&lt;i&gt;&lt;strong&gt;System.Collections.Generic.IDictionary&lt;/strong&gt;&lt;/i&gt;&amp;lt;TIndex, T&amp;gt;, while the field is initialized with an object of the &lt;i&gt;&lt;strong&gt;System.Collections.Generic.Dictionary&lt;/strong&gt;&lt;/i&gt;&amp;lt;TIndex, T&amp;gt;, where TIndex is the type of the collection index, and &amp;nbsp;T is the details entity.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Array&lt;/strong&gt; maps to an &lt;i&gt;array of collection elements&lt;/i&gt;(T[] for C#). &amp;nbsp;The field of the class, where the the collection is stored, is initialized as an array of collection elements.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The initialization of the field in the class constructor, where the collection is stores, is declarative and is performed in order to prevent the null reference exception when the property of a newly created class object is called. When data are obtained from the database and mapped to objects, NHibernate run-time re-initializes the field and fills in the collection.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Changing Values in NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/0.aspx</link><pubDate>Mon, 08 Aug 2011 15:20:19 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:89</guid><dc:creator>Stefan Steinegger</dc:creator><description>Current revision posted to How to by Stefan Steinegger on 08/08/2011 12:20:19 p.m.&lt;br /&gt;
&lt;h2&gt;Changing Values in &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listener&lt;/span&gt;&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; Although It may work in some cases and
is sometimes referred to as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;&lt;/p&gt;
&lt;p&gt;There is also a bug report about that.&lt;/p&gt;
&lt;h2&gt;Why OnPreUpdate Triggers May Fail&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;There is a reason for that. NHibernate does the folowing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When flushing, the FlushEntityEventListener is called for each entity in the session.&lt;/li&gt;
&lt;li&gt;It determines the dirty properties.&lt;/li&gt;
&lt;li&gt;Based on the dirty properties, it determines if the entity needs updating&lt;/li&gt;
&lt;li&gt;It calls PreUpdate triggers&lt;/li&gt;
&lt;li&gt;Based on the dirty properties it updates the corresponding tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last point is the critical one. If your PreUpdate trigger changes properties that are not found dirty before and which are not in the same table as the dirty properties, it won&amp;#39;t be updated in the database. The same happens when dynamic-update is used.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;p&gt;&amp;lt;class name=&amp;quot;Foo&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;id ...&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;property name=&amp;quot;Name&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;join table=&amp;quot;AnotherTable&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;key name=&amp;quot;id&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Value&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/p&gt;
&lt;p&gt;Now, when you change Name in the session (it gets dirty) and Value in the trigger, the table AnotherTable won&amp;#39;t be updated.&lt;/p&gt;
&lt;h2&gt;How To Implement It The Correct Way&lt;/h2&gt;
&lt;p&gt;The correct way is to use the &lt;b&gt;FlushEntityEventListener &lt;/b&gt;to implement property &lt;span style="text-decoration: line-through; color: red;"&gt;changes&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;changes.The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;important&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;difference&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listener&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;therefore&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tell&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;store&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;There is an &lt;a href="https://github.com/Buthrakaur/NHListenerTests/blob/master/NHListenerTest/SetModificationTimeFlushEntityEventListener.cs"&gt;example by Buthrakaur&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;History&lt;/h2&gt;
&lt;p&gt;There is a solution to reproduce the code by Scott Findlater&lt;span class="fontsize2 author"&gt;&lt;span style="color:#00681C;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;here&lt;/a&gt;. The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h3&gt;References to the problem&lt;br /&gt;&lt;/h3&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is also &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the resolution of &amp;quot;not an issue&amp;quot; because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;p&gt;There are some discussions and blog posts related to this problem:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;audit listener using IFlushEntityEventListener (continuation of NH-2596)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;Bizarre error when using NHibernate, IPreInsertEventListener, Oracle, batching and sequences &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;Clarification on NH-2596 of PreUpdate vs OnSaveOrUpdate semantics &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Changing Values in OnPreUpdate Events</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/10.aspx</link><pubDate>Mon, 08 Aug 2011 15:17:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:506</guid><dc:creator>Stefan Steinegger</dc:creator><description>Revision 10 posted to How to by Stefan Steinegger on 08/08/2011 12:17:25 p.m.&lt;br /&gt;
&lt;h2&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Audit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;trails&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreInsert&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Values&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; OnPreUpdate &lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listeners&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;vs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Save&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listeners&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Events&lt;/span&gt;&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Audit&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;While&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;does&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;works&lt;/span&gt; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Although&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;may&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;work&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cases&lt;/span&gt; and
is &lt;span style="text-decoration: line-through; color: red;"&gt;widely&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sometimes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;referred&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;veto&amp;quot;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Code&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;veto&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;report&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="background: SpringGreen;"&gt;Why&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Triggers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;May&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fail&lt;/span&gt;&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reason&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;does&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folowing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;When&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;flushing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FlushEntityEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;determines&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Based&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;determines&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;needs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updating&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;calls&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;triggers&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Based&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updates&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;corresponding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tables&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;span style="text-decoration: line-through; color: red;"&gt;full&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;last&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;point&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;critical&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trigger&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;won&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;t&lt;/span&gt; be &lt;span style="text-decoration: line-through; color: red;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;happens&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dynamic-update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Example&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Foo&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;id&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;..&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Name&amp;quot;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;join&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;AnotherTable&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;id&amp;quot;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Value&amp;quot;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;/join&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/class&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;gets&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Value&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trigger&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;AnotherTable&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;won&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;t&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updated&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="background: SpringGreen;"&gt;How&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;To&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Correct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Way&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;way&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FlushEntityEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changes&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;a href="https://github.com/Buthrakaur/NHListenerTests/blob/master/NHListenerTest/SetModificationTimeFlushEntityEventListener.cs"&gt;&lt;span style="background: SpringGreen;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Buthrakaur&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="background: SpringGreen;"&gt;History&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reproduce&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Scott&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Findlater&lt;/span&gt;&lt;span class="fontsize2 author"&gt;&lt;span style="color:#00681C;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;here&lt;/a&gt;. The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;May&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;politely&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ask&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bear&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mind&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;aim&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;issue&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;inheritance&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;they&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;been&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;over&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;simplified&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;auditing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;mind&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demonstrate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inheritance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;issues&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;within&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;..&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;commercial&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;design&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pattern&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;History&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;section&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;history&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;arriving&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;post&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;Please&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;read&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;spirit&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sharing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;attempting&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contribute&lt;/span&gt; &lt;/p&gt;
&lt;h3&gt;&lt;span style="background: SpringGreen;"&gt;References&lt;/span&gt; to &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt;&lt;br /&gt;&lt;/h3&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;And&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;there&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; is &lt;span style="text-decoration: line-through; color: red;"&gt;no&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;doubt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;these&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solutions&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;work&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;concept&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;changing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;state&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;before&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;pre)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;insertion&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;updating&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;adopted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;approach&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;auditing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;within&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inheritance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;architected&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;when&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;found&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;what&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;thought&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bug&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;subsequently&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ended&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;up&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the &lt;span style="text-decoration: line-through; color: red;"&gt;unexpected&lt;/span&gt;
resolution of &amp;quot;not an issue&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;.&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;proposed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Save&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listeners&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;obviously&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;works&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;unexpected&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reason&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;why&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;report&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bug&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Conclusion&lt;/span&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;note&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Fabio&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;clearly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;states&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;usage&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPre&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;herewith&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; some &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;However&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;perspective&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;developer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;learning&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;feel&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contradicted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;key&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;community&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;people&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;am&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;yet&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;discussions&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;posts&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;related&lt;/span&gt; to &lt;span style="text-decoration: line-through; color: red;"&gt;truly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;understand&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;why&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;variety&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;scenarios&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demonstrated&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; this &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;considered&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bug&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;Maybe&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;there&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;some&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;explicit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;enforcing&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;being&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;able&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;change&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;state&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;these&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPre&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;A&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;detailed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;example&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Fabio&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;?&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;There&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;seem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;alot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cries&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;help&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;no&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;conclusive&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;working&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;examples&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;audit listener using IFlushEntityEventListener (continuation of NH-2596)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;Bizarre error when using NHibernate, IPreInsertEventListener, Oracle, batching and sequences &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;Clarification on NH-2596 of PreUpdate vs OnSaveOrUpdate semantics &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;And&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Fabio&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;delivers&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;blog&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;post&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/9.aspx</link><pubDate>Thu, 12 May 2011 08:47:48 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:505</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 9 posted to How to by Scott Findlater on 12/05/2011 05:47:48 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;History&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;This section is the history of arriving at this
post.&amp;nbsp; Please read this with the spirit
of sharing and attempting to contribute to documentation.&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And there is no doubt all of these solutions work
using the concept of changing the state of the entity before (pre) database
insertion or updating.&lt;/p&gt;
&lt;p&gt;I adopted the OnPreInsert and OnPreUpdate approach
for an auditing solution within an inheritance architected solution when I found,
what I thought, was an NHibernate bug.&amp;nbsp; I
subsequently ended up at &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the unexpected
resolution of &amp;quot;not an issue&amp;quot;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The solution proposed was to use the Save/ Update
event listeners, which obviously works, but the unexpected reason as to why
this report was not a bug was because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;I note Fabio clearly states the usage for the OnPre*
events and herewith is some documentation.&amp;nbsp;
However, from the perspective of a developer learning NHibernate, I feel
this is contradicted by key NHibernate community people.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I am yet to truly understand why, with the variety
of scenarios demonstrated, this is a not considered a bug.&amp;nbsp; Maybe there should be some explicit enforcing
of not being able to change entity state in these OnPre* events.&lt;/p&gt;
&lt;h3&gt;A detailed example from Fabio?&lt;/h3&gt;
&lt;p&gt;There seem to be alot of cries for help but with no conclusive working examples;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;audit listener using IFlushEntityEventListener (continuation of NH-2596)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;Bizarre error when using NHibernate, IPreInsertEventListener, Oracle, batching and sequences &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;Clarification on NH-2596 of PreUpdate vs OnSaveOrUpdate semantics &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style="background: SpringGreen;"&gt;And&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;delivers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html"&gt;&lt;span style="background: SpringGreen;"&gt;http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/8.aspx</link><pubDate>Wed, 11 May 2011 12:29:26 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:464</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 8 posted to How to by Scott Findlater on 11/05/2011 09:29:26 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;History&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;This section is the history of arriving at this
post.&amp;nbsp; Please read this with the spirit
of sharing and attempting to contribute to documentation.&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And there is no doubt all of these solutions work
using the concept of changing the state of the entity before (pre) database
insertion or updating.&lt;/p&gt;
&lt;p&gt;I adopted the OnPreInsert and OnPreUpdate approach
for an auditing solution within an inheritance architected solution when I found,
what I thought, was an NHibernate bug.&amp;nbsp; I
subsequently ended up at &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the unexpected
resolution of &amp;quot;not an issue&amp;quot;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The solution proposed was to use the Save/ Update
event listeners, which obviously works, but the unexpected reason as to why
this report was not a bug was because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;I note Fabio clearly states the usage for the OnPre*
events and herewith is some documentation.&amp;nbsp;
However, from the perspective of a developer learning NHibernate, I feel
this is contradicted by key NHibernate community people.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I am yet to truly understand why, with the variety
of scenarios demonstrated, this is a not considered a bug.&amp;nbsp; Maybe there should be some explicit enforcing
of not being able to change entity state in these OnPre* events.&lt;/p&gt;
&lt;h3&gt;&lt;span style="background: SpringGreen;"&gt;A&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;detailed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;seem&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;alot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cries&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;help&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;no&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;conclusive&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;examples&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;&lt;span style="background: SpringGreen;"&gt;audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IFlushEntityEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;continuation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NH-2596&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;&lt;span style="background: SpringGreen;"&gt;Bizarre&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;error&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreInsertEventListener&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Oracle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;batching&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sequences&lt;/span&gt; &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;&lt;span style="background: SpringGreen;"&gt;Clarification&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NH-2596&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;vs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnSaveOrUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;semantics&lt;/span&gt; &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/7.aspx</link><pubDate>Wed, 27 Apr 2011 05:32:27 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:463</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 7 posted to How to by Scott Findlater on 27/04/2011 02:32:27 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;History&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;This section is the history of arriving at this
post.&amp;nbsp; Please read this with the spirit
of sharing and attempting to contribute to documentation.&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And there is no doubt all of these solutions work
using the concept of changing the state of the entity before (pre) database
insertion or updating.&lt;/p&gt;
&lt;p&gt;I adopted the OnPreInsert and OnPreUpdate approach
for an auditing solution within an inheritance architected solution when I found,
what I thought, was an NHibernate bug.&amp;nbsp; I
subsequently ended up at &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the unexpected
resolution of &amp;quot;not an issue&amp;quot;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The solution proposed was to use the Save/ Update
event listeners, which obviously works, but the unexpected reason as to why
this report was not a bug was because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Conclusion&lt;/span&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;note&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;clearly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;states&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;usage&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPre&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;*&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;herewith&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;documentation&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp;
&lt;span style="background: SpringGreen;"&gt;However&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;perspective&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;developer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;learning&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;feel&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contradicted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;community&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;people&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;am&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;yet&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;truly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;understand&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;why&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;variety&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;scenarios&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demonstrated&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;considered&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Maybe&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;there&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;explicit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;enforcing&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;being&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;able&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;these&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPre&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;*&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/6.aspx</link><pubDate>Tue, 26 Apr 2011 23:42:34 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:459</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 6 posted to How to by Scott Findlater on 26/04/2011 08:42:34 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;History&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;history&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;arriving&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Please&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;read&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;spirit&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sharing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;attempting&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contribute&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;documentation&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trails&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;model&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;well&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;documented&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Ayende&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreUpdateEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;amp;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreInsertEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Graham&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Bunce&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Log&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Events&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;WIKI&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;span style="background: SpringGreen;"&gt;And&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;most&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Jason&lt;/span&gt; &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;&lt;span style="background: SpringGreen;"&gt;Dentler&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3.0&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cookbook&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;chapter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;7&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;stamping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entities&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recipe&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;And&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;there&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;no&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;doubt&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;these&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solutions&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;work&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;concept&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;pre)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;insertion&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updating&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;adopted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;approach&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;within&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;architected&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thought&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;subsequently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;up&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;report&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;made&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreInsert/UpdateEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;persisted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;into&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;DB&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inherited&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;classes&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unexpected&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;resolution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;.&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proposed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Save&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listeners&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;obviously&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;works&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unexpected&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reason&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;why&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;report&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;because&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;i&gt;&lt;span style="background: SpringGreen;"&gt;pre-insert&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;pre-update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listeners&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;intended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;values&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Instead&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check-values&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reason&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;veto&amp;quot;).&amp;quot;&lt;/span&gt;&lt;/i&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;&lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;context&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contradiction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;intended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;usage&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;regarding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;further&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;discussed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;&lt;span style="background: SpringGreen;"&gt;NHUser&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Group&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thread&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;answer&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/5.aspx</link><pubDate>Tue, 26 Apr 2011 23:40:44 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:458</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 5 posted to How to by Scott Findlater on 26/04/2011 08:40:44 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;03Save&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Uses&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hierarchy&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;02OnPreEventsFailing&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demonstrate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Save&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Insert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/4.aspx</link><pubDate>Tue, 26 Apr 2011 23:40:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:457</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 4 posted to How to by Scott Findlater on 26/04/2011 08:40:12 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
&lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;02OnPreEventsFailing&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;shows&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;hierarchy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;modify&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;fail&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;persist&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp;
&lt;span style="background: SpringGreen;"&gt;Here&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whatever&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;design&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;decision&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;base&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;originally&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;identification&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;common&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;been&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;split&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;into&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt;
&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;AuditableEntity&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt;&lt;/b&gt;
&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inherits&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;AuditableEntity&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/3.aspx</link><pubDate>Tue, 26 Apr 2011 23:39:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:456</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 3 posted to How to by Scott Findlater on 26/04/2011 08:39:37 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Code&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;full&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;&lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;tests&lt;/span&gt;&lt;/b&gt;
&lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;highlights&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;successes&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issues&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;failing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tests&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;May&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;politely&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ask&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bear&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mind&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;aim&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;been&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;over&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simplified&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;mind&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demonstrate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issues&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;within&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;..&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;commercial&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;design&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;pattern&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
&lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;01OnPreEvents&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;shows&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;typical&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;approach&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trails&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;modify&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;An&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;base&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inherited&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;base&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;provides&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;identification&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;common&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/2.aspx</link><pubDate>Tue, 26 Apr 2011 23:37:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:455</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 2 posted to How to by Scott Findlater on 26/04/2011 08:37:53 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/1.aspx</link><pubDate>Tue, 26 Apr 2011 23:37:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:454</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 1 posted to How to by Scott Findlater on 26/04/2011 08:37:53 p.m.&lt;br /&gt;
&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>View the xml generated when mapping by code</title><link>http://nhforge.org/wikis/howtonh/view-the-xml-generated-when-mapping-by-code/revision/0.aspx</link><pubDate>Tue, 26 Jul 2011 12:42:55 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:92</guid><dc:creator>rippo</dc:creator><description>Current revision posted to How to by rippo on 26/07/2011 09:42:55 a.m.&lt;br /&gt;
&lt;h2&gt;View the xml generated when mapping by code&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Architecture&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;When using mapping by code it may not be 100% obvious what XML is being generated for NHibernate.
You have a couple of options, the first option is to tell NHibernate to write all your mappings into the bin folder:-
&lt;/p&gt;
&lt;pre class="csharp"&gt;var mapper = new ModelMapper();
mapper.AddMappings(typeof(CmsMeta).Assembly.GetTypes());

//This will write all the XML into the bin/mappings folder
mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping();
&lt;/pre&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-dAeDt7mJGn4/Ti0dtPRmBZI/AAAAAAAAACE/iAiXfzBiIM0/s1600/Capture.PNG"&gt;&lt;img border="0" height="138" src="http://2.bp.blogspot.com/-dAeDt7mJGn4/Ti0dtPRmBZI/AAAAAAAAACE/iAiXfzBiIM0/s640/Capture.PNG" width="535" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
The second option is to use the extension method &lt;em&gt;.AsString()&lt;/em&gt;. However you will be presented with one large XML blob.
&lt;/p&gt;
&lt;pre class="csharp"&gt;var mapper = new ModelMapper();
mapper.AddMappings(typeof(CmsMeta).Assembly.GetTypes());
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
//you could add a breakpoint here! 
var mappingXml = mapping.AsString();
&lt;/pre&gt;
&lt;p&gt;
Either way you will be able to quickly and easily view the XML that is being injected into NHibernate. This is very useful for debugging or if you are upgrading all your hbm.xml files into the new mapping by code syntax and wish to compare like for like.&lt;/p&gt;</description></item><item><title>View the xml generated when mapping by code</title><link>http://nhforge.org/wikis/howtonh/view-the-xml-generated-when-mapping-by-code/revision/1.aspx</link><pubDate>Tue, 26 Jul 2011 12:42:55 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:504</guid><dc:creator>rippo</dc:creator><description>Revision 1 posted to How to by rippo on 26/07/2011 09:42:55 a.m.&lt;br /&gt;
&lt;p&gt;When using mapping by code it may not be 100% obvious what XML is being generated for NHibernate.
You have a couple of options, the first option is to tell NHibernate to write all your mappings into the bin folder:-
&lt;/p&gt;
&lt;pre class="csharp"&gt;var mapper = new ModelMapper();
mapper.AddMappings(typeof(CmsMeta).Assembly.GetTypes());

//This will write all the XML into the bin/mappings folder
mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping();
&lt;/pre&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-dAeDt7mJGn4/Ti0dtPRmBZI/AAAAAAAAACE/iAiXfzBiIM0/s1600/Capture.PNG"&gt;&lt;img border="0" height="138" src="http://2.bp.blogspot.com/-dAeDt7mJGn4/Ti0dtPRmBZI/AAAAAAAAACE/iAiXfzBiIM0/s640/Capture.PNG" width="535" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;
The second option is to use the extension method &lt;em&gt;.AsString()&lt;/em&gt;. However you will be presented with one large XML blob.
&lt;/p&gt;
&lt;pre class="csharp"&gt;var mapper = new ModelMapper();
mapper.AddMappings(typeof(CmsMeta).Assembly.GetTypes());
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
//you could add a breakpoint here! 
var mappingXml = mapping.AsString();
&lt;/pre&gt;
&lt;p&gt;
Either way you will be able to quickly and easily view the XML that is being injected into NHibernate. This is very useful for debugging or if you are upgrading all your hbm.xml files into the new mapping by code syntax and wish to compare like for like.&lt;/p&gt;</description></item><item><title>ConventionModelMapper - change ManyToOne column name</title><link>http://nhforge.org/wikis/howtonh/conventionmodelmapper-change-manytoone-column-name/revision/0.aspx</link><pubDate>Thu, 14 Jul 2011 05:37:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:91</guid><dc:creator>ashern</dc:creator><description>Current revision posted to How to by ashern on 14/07/2011 02:37:37 a.m.&lt;br /&gt;
&lt;h2&gt;ConventionModelMapper - change ManyToOne column name&lt;/h2&gt;
&lt;h4&gt;Creating a convention to override the default column naming for your ManyToOne relationships is simple, as follows:&lt;/h4&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;mapper.BeforeMapManyToOne += (insp, prop, map) =&amp;gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;map.Column(prop.LocalMember.GetPropertyOrFieldType().Name +&amp;nbsp;&lt;span&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;span&gt;&lt;span&gt;But, if any of the above are bi-directional relationships, be sure to equivalently map the inverse side&amp;#39;s key as required for your collection types:&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;i&gt;for set&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;mapper.BeforeMapSet&amp;nbsp;+=&amp;nbsp;(insp,&amp;nbsp;typ,&amp;nbsp;map)&amp;nbsp;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;km.Column(typ.GetContainerEntity(insp).Name&amp;nbsp;+&amp;nbsp;&amp;quot;Id&amp;quot;))&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;       &lt;span style="background: SpringGreen;"&gt;km.Column(typ.GetContainerEntity(insp).Name&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Id&amp;quot;))&lt;/span&gt;&lt;i&gt;;&lt;/i&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="white-space:pre;"&gt;&lt;i&gt;for bag&lt;/i&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;pre&gt;mapper.BeforeMapBag&amp;nbsp;+=&amp;nbsp;(insp,&amp;nbsp;typ,&amp;nbsp;map)&amp;nbsp;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;km.Column(typ.GetContainerEntity(insp).Name&amp;nbsp;+&amp;nbsp;&amp;quot;Id&amp;quot;))&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;       &lt;span style="background: SpringGreen;"&gt;km.Column(typ.GetContainerEntity(insp).Name&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Id&amp;quot;))&lt;/span&gt;&lt;i&gt;;&lt;/i&gt;&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;etc...&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>ConventionModelMapper - change ManyToOne column name</title><link>http://nhforge.org/wikis/howtonh/conventionmodelmapper-change-manytoone-column-name/revision/2.aspx</link><pubDate>Thu, 14 Jul 2011 05:34:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:503</guid><dc:creator>ashern</dc:creator><description>Revision 2 posted to How to by ashern on 14/07/2011 02:34:54 a.m.&lt;br /&gt;
&lt;h2&gt;ConventionModelMapper - change ManyToOne column name&lt;/h2&gt;
&lt;h4&gt;Creating a convention to override the default column naming for your ManyToOne relationships is simple, as follows:&lt;/h4&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;mapper.BeforeMapManyToOne += (insp, prop, map) =&amp;gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;map.Column(prop.LocalMember&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;wbr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;GetPropertyOrFieldType().Name&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style="background: SpringGreen;"&gt;map.Column(prop.LocalMember.GetPropertyOrFieldType().Name&lt;/span&gt; +&amp;nbsp;&lt;span&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;span&gt;&lt;span&gt;But, if any of the above are bi-directional relationships, be sure to equivalently map the inverse side&amp;#39;s key as required for your collection types:&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;i&gt;for set&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;mapper.BeforeMapSet&amp;nbsp;+=&amp;nbsp;(insp,&amp;nbsp;typ,&amp;nbsp;map)&amp;nbsp;=&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;km.Column(typ.GetContainerEntity(insp).Name&amp;nbsp;+&amp;nbsp;&amp;quot;Id&amp;quot;))&lt;i&gt;;&lt;/i&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="white-space:pre;"&gt;&lt;i&gt;for bag&lt;/i&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;pre&gt;mapper.BeforeMapBag&amp;nbsp;+=&amp;nbsp;(insp,&amp;nbsp;typ,&amp;nbsp;map)&amp;nbsp;=&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;km.Column(typ.GetContainerEntity(insp).Name&amp;nbsp;+&amp;nbsp;&amp;quot;Id&amp;quot;))&lt;i&gt;;&lt;/i&gt;&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;etc...&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>ConventionModelMapper - change ManyToOne column name</title><link>http://nhforge.org/wikis/howtonh/conventionmodelmapper-change-manytoone-column-name/revision/1.aspx</link><pubDate>Thu, 14 Jul 2011 05:34:07 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:502</guid><dc:creator>ashern</dc:creator><description>Revision 1 posted to How to by ashern on 14/07/2011 02:34:07 a.m.&lt;br /&gt;
&lt;h4&gt;Creating a convention to override the default column naming for your ManyToOne relationships is simple, as follows:&lt;/h4&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;mapper.BeforeMapManyToOne += (insp, prop, map) =&amp;gt; &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;map.Column(prop.LocalMember.&amp;lt;wbr /&amp;gt;GetPropertyOrFieldType().Name +&amp;nbsp;&lt;span&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;span&gt;&lt;span&gt;But, if any of the above are bi-directional relationships, be sure to equivalently map the inverse side&amp;#39;s key as required for your collection types:&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;i&gt;for set&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;mapper.BeforeMapSet&amp;nbsp;+=&amp;nbsp;(insp,&amp;nbsp;typ,&amp;nbsp;map)&amp;nbsp;=&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;km.Column(typ.GetContainerEntity(insp).Name&amp;nbsp;+&amp;nbsp;&amp;quot;Id&amp;quot;))&lt;i&gt;;&lt;/i&gt;&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span style="white-space:pre;"&gt;&lt;i&gt;for bag&lt;/i&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;
&lt;pre&gt;mapper.BeforeMapBag&amp;nbsp;+=&amp;nbsp;(insp,&amp;nbsp;typ,&amp;nbsp;map)&amp;nbsp;=&amp;gt;&amp;nbsp;map.Key(km&amp;nbsp;=&amp;gt;&amp;nbsp;km.Column(typ.GetContainerEntity(insp).Name&amp;nbsp;+&amp;nbsp;&amp;quot;Id&amp;quot;))&lt;i&gt;;&lt;/i&gt;&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;etc...&lt;/i&gt;&lt;/p&gt;
&lt;/p&gt;</description></item><item><title>ConventionModelMapper &amp; .net 4 ISet&lt;&gt;</title><link>http://nhforge.org/wikis/howtonh/conventionmodelmapper-amp-net-4-iset-lt-gt/revision/0.aspx</link><pubDate>Thu, 14 Jul 2011 05:19:16 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:90</guid><dc:creator>ashern</dc:creator><description>Current revision posted to How to by ashern on 14/07/2011 02:19:16 a.m.&lt;br /&gt;
&lt;h2&gt;ConventionModelMapper &amp;amp; .net 4 ISet&amp;lt;&amp;gt;&lt;/h2&gt;
&lt;p&gt;For anyone using&amp;nbsp;&lt;span&gt;Jos&amp;eacute;&amp;#39;s Net4CollectionTypeFactory to enable native ISet&amp;lt;&amp;gt; usage, here&amp;#39;s how to get the convention mapper to recognize your ISet&amp;lt;&amp;gt; properties as type Set rather than Bag (code courtesy JFR):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;In your mapping&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;mapper.IsSet(IsSetFieldType);&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Function implementation&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre&gt;&lt;span&gt;private&lt;/span&gt;&amp;nbsp;&lt;span&gt;static&lt;/span&gt;&amp;nbsp;&lt;span&gt;bool&lt;/span&gt;&amp;nbsp;IsSetFieldType(&lt;span&gt;MemberInfo&lt;/span&gt;&amp;nbsp;mi,&amp;nbsp;&lt;span&gt;bool&lt;/span&gt;&amp;nbsp;declared)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;var&lt;/span&gt;&amp;nbsp;propertyTypeIsSet&amp;nbsp;=&amp;nbsp;mi.GetPropertyOrFieldType()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.GetGenericInterfaceTypeDefinitions()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Contains(&lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ISet&lt;/span&gt;&amp;lt;&amp;gt;));
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;if&lt;/span&gt;&amp;nbsp;(propertyTypeIsSet)&amp;nbsp;&lt;span&gt;return&lt;/span&gt;&amp;nbsp;&lt;span&gt;true&lt;/span&gt;;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;var&lt;/span&gt;&amp;nbsp;backFieldInfo&amp;nbsp;=&amp;nbsp;&lt;span&gt;PropertyToField&lt;/span&gt;.GetBackFieldInfo((&lt;span&gt;PropertyInfo&lt;/span&gt;)mi);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;return&lt;/span&gt;&amp;nbsp;backFieldInfo&amp;nbsp;!=&amp;nbsp;&lt;span&gt;null&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;backFieldInfo
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.FieldType.GetGenericInterfaceTypeDefinitions().Contains(&lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ISet&lt;/span&gt;&amp;lt;&amp;gt;));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style="text-decoration:underline;"&gt;Remember to add the collection type factory to your config..&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;wbr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;CollectionTypeFactory&amp;lt;Net4CollectionTypeFactory&amp;gt;();&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;&lt;span style="background: SpringGreen;"&gt;configuration.CollectionTypeFactory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Net4CollectionTypeFactory&amp;gt;();&lt;/span&gt;&lt;/pre&gt;</description></item><item><title>ConventionModelMapper &amp; .net 4 ISet&lt;&gt;</title><link>http://nhforge.org/wikis/howtonh/conventionmodelmapper-amp-net-4-iset-lt-gt/revision/2.aspx</link><pubDate>Thu, 14 Jul 2011 05:18:27 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:501</guid><dc:creator>ashern</dc:creator><description>Revision 2 posted to How to by ashern on 14/07/2011 02:18:27 a.m.&lt;br /&gt;
&lt;h2&gt;ConventionModelMapper &amp;amp; .net 4 ISet&amp;lt;&amp;gt;&lt;/h2&gt;
&lt;p&gt;For anyone using&amp;nbsp;&lt;span&gt;Jos&amp;eacute;&amp;#39;s Net4CollectionTypeFactory to enable native ISet&amp;lt;&amp;gt; usage, here&amp;#39;s how to get the convention mapper to recognize your ISet&amp;lt;&amp;gt; properties as type Set rather than Bag (code courtesy JFR):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;In your mapping&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;mapper.IsSet(IsSetFieldType);&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Function implementation&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre&gt;&lt;span&gt;private&lt;/span&gt;&amp;nbsp;&lt;span&gt;static&lt;/span&gt;&amp;nbsp;&lt;span&gt;bool&lt;/span&gt;&amp;nbsp;IsSetFieldType(&lt;span&gt;MemberInfo&lt;/span&gt;&amp;nbsp;mi,&amp;nbsp;&lt;span&gt;bool&lt;/span&gt;&amp;nbsp;declared)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;var&lt;/span&gt;&amp;nbsp;propertyTypeIsSet&amp;nbsp;=&amp;nbsp;mi.GetPropertyOrFieldType()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.GetGenericInterfaceTypeDefinitions()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Contains(&lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ISet&lt;/span&gt;&amp;lt;&amp;gt;));
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;if&lt;/span&gt;&amp;nbsp;(propertyTypeIsSet)&amp;nbsp;&lt;span&gt;return&lt;/span&gt;&amp;nbsp;&lt;span&gt;true&lt;/span&gt;;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;var&lt;/span&gt;&amp;nbsp;backFieldInfo&amp;nbsp;=&amp;nbsp;&lt;span&gt;PropertyToField&lt;/span&gt;.GetBackFieldInfo((&lt;span&gt;PropertyInfo&lt;/span&gt;)mi);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;return&lt;/span&gt;&amp;nbsp;backFieldInfo&amp;nbsp;!=&amp;nbsp;&lt;span&gt;null&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;backFieldInfo
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.FieldType.GetGenericInterfaceTypeDefinitions().Contains(&lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ISet&lt;/span&gt;&amp;lt;&amp;gt;));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style="text-decoration:underline;"&gt;Remember to add the collection type factory to your config..&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;configuration.&amp;lt;wbr /&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;CollectionTypeFactory&amp;lt;&amp;lt;wbr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;Net4CollectionTypeFactory&amp;gt;();&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;CollectionTypeFactory&amp;lt;Net4CollectionTypeFactory&amp;gt;();&lt;/span&gt;&lt;/pre&gt;</description></item><item><title>ConventionModelMapper &amp; .net 4 ISet&lt;&gt;</title><link>http://nhforge.org/wikis/howtonh/conventionmodelmapper-amp-net-4-iset-lt-gt/revision/1.aspx</link><pubDate>Thu, 14 Jul 2011 05:16:32 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:500</guid><dc:creator>ashern</dc:creator><description>Revision 1 posted to How to by ashern on 14/07/2011 02:16:32 a.m.&lt;br /&gt;
&lt;p&gt;For anyone using&amp;nbsp;&lt;span&gt;Jos&amp;eacute;&amp;#39;s Net4CollectionTypeFactory to enable native ISet&amp;lt;&amp;gt; usage, here&amp;#39;s how to get the convention mapper to recognize your ISet&amp;lt;&amp;gt; properties as type Set rather than Bag (code courtesy JFR):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;In your mapping&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;mapper.IsSet(IsSetFieldType);&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Function implementation&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre&gt;&lt;span&gt;private&lt;/span&gt;&amp;nbsp;&lt;span&gt;static&lt;/span&gt;&amp;nbsp;&lt;span&gt;bool&lt;/span&gt;&amp;nbsp;IsSetFieldType(&lt;span&gt;MemberInfo&lt;/span&gt;&amp;nbsp;mi,&amp;nbsp;&lt;span&gt;bool&lt;/span&gt;&amp;nbsp;declared)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;var&lt;/span&gt;&amp;nbsp;propertyTypeIsSet&amp;nbsp;=&amp;nbsp;mi.GetPropertyOrFieldType()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.GetGenericInterfaceTypeDefinitions()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Contains(&lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ISet&lt;/span&gt;&amp;lt;&amp;gt;));
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;if&lt;/span&gt;&amp;nbsp;(propertyTypeIsSet)&amp;nbsp;&lt;span&gt;return&lt;/span&gt;&amp;nbsp;&lt;span&gt;true&lt;/span&gt;;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;var&lt;/span&gt;&amp;nbsp;backFieldInfo&amp;nbsp;=&amp;nbsp;&lt;span&gt;PropertyToField&lt;/span&gt;.GetBackFieldInfo((&lt;span&gt;PropertyInfo&lt;/span&gt;)mi);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span&gt;return&lt;/span&gt;&amp;nbsp;backFieldInfo&amp;nbsp;!=&amp;nbsp;&lt;span&gt;null&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;backFieldInfo
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.FieldType.GetGenericInterfaceTypeDefinitions().Contains(&lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ISet&lt;/span&gt;&amp;lt;&amp;gt;));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;
&lt;pre&gt;&lt;span style="text-decoration:underline;"&gt;Remember to add the collection type factory to your config..&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;configuration.&amp;lt;wbr /&amp;gt;CollectionTypeFactory&amp;lt;&amp;lt;wbr /&amp;gt;Net4CollectionTypeFactory&amp;gt;();&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/0.aspx</link><pubDate>Fri, 08 Jul 2011 22:54:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:16</guid><dc:creator>MarkCranness</dc:creator><description>Current revision posted to How to by MarkCranness on 08/07/2011 07:54:56 p.m.&lt;br /&gt;
&lt;h2&gt;Lazy loading - eager loading&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;loading&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this article I want to discuss the&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt;&amp;nbsp;mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them. &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&lt;/a&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In my previous posts I showed&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles. &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/LazyLoadingEagerLoadingDomain.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the&amp;nbsp;&lt;b&gt;Build Action&lt;/b&gt;&amp;nbsp;of each mapping file to&amp;nbsp;&lt;b&gt;Embedded Resource&lt;/b&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;b&gt;hbm&lt;/b&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the&amp;nbsp;&lt;b&gt;Order&lt;/b&gt;&amp;nbsp;entity might be implemented as follows&lt;/p&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;set &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; cascade&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;key &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;one-to-many &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;set&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;p&gt;Analogous you can implement the mappings for the&amp;nbsp;&lt;b&gt;Customer&lt;/b&gt;&amp;nbsp;entity&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:3acfe276-b736-4c58-88ae-7c262976b50f" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and finally the mapping for the&amp;nbsp;&lt;b&gt;OrderLine&lt;/b&gt;&amp;nbsp;entity.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:23053d0d-5f88-44d1-8099-ff8406d62f4e" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;max-height:370px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt;&amp;nbsp;previous post for further details about the configuration file. &lt;/p&gt;
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt;&amp;nbsp;principle) we implement the following base class.&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:866px;max-height:866px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        [SetUp]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            After_each_test();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;b&gt;TestFixtureSetUp&lt;/b&gt;). When the whole test fixture is ended the session factory is closed (&lt;b&gt;TestFixtureTearDown&lt;/b&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual&amp;nbsp;&lt;b&gt;Before_each_test&lt;/b&gt;&amp;nbsp;method is called. After each test in the fixture is finished the virtual&amp;nbsp;&lt;b&gt;After_each_test&lt;/b&gt;&amp;nbsp;method is called. The two virtual methods can (but must not necessarily) be overridden in a child class.&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class&amp;nbsp;&lt;b&gt;Order_Fixture&lt;/b&gt;&amp;nbsp;to our test project and inherit from the&amp;nbsp;&lt;b&gt;TestFixtureBase&lt;/b&gt;&amp;nbsp;base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;max-height:749px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            &lt;span style="color:#008000;"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            var customer = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span style="color:#006080;"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            var line1 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            var line2 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _order = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                        {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                            OrderNumber = &lt;span style="color:#006080;"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                        };&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The&amp;nbsp;&lt;b&gt;CreateInitialData&lt;/b&gt;&amp;nbsp;method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:170px;max-height:200px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
 
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:109px;max-height:200px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
    order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
    order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
    order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:202px;max-height:200px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;[Test]
[ExpectedException(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(LazyInitializationException))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
    
    &lt;span style="color:#008000;"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// Note: at this point the session is already closed&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test&amp;nbsp;&lt;b&gt;only&lt;/b&gt;&amp;nbsp;succeeds if the method throws the expected exception of type&amp;nbsp;&lt;b&gt;LazyInitializationException&lt;/b&gt;. And this is just what we want to show!&lt;/p&gt;
&lt;p&gt;Another problem is the&amp;nbsp;&lt;b&gt;n+1 select statements problem&lt;/b&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:233px;max-height:200px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()
{
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
    {
        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
        &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; sum = 0;
        &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var line &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb.OrderLines)
        {
            &lt;span style="color:#008000;"&gt;// just some dummy code to force loading of order line&lt;/span&gt;
            sum += line.Amount;
        } 
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:235px;max-height:200px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id3_0_, 
       order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_3_0_, 
       order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate3_0_, 
       order0_.Customer &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Customer3_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;
 
&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; orderlines0_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id4_0_, 
       orderlines0_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount4_0_, 
       orderlines0_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_4_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   OrderLine orderlines0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  orderlines0_.OrderId=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the&amp;nbsp;&lt;b&gt;NHibernateUtil&lt;/b&gt;&amp;nbsp;class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:444px;max-height:444px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:286px;max-height:286px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; sql = &lt;span style="color:#006080;"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                        .SetGuid(&lt;span style="color:#006080;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="line-height:12pt;background-color:white;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;div style="line-height:12pt;background-color:#f4f4f4;margin:20px 0px 10px;width:97.5%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:261px;max-height:260px;font-size:8pt;overflow:auto;cursor:text;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="line-height:12pt;background-color:#f4f4f4;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;      order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_1_, 
            customer2_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id2_2_, 
            order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
            order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
            order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_, 
            orderlines1_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount1_1_, 
            orderlines1_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_1_1_, 
            customer2_.CompanyName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CompanyN2_2_2_, 
            orderlines1_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId0__, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0__ 
&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;        Orders order0_ 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id 
&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;       order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely&amp;nbsp;&lt;b&gt;Orders&lt;/b&gt;,&amp;nbsp;&lt;b&gt;Customer&lt;/b&gt;&amp;nbsp;and&lt;b&gt;OrderLine&lt;/b&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of&amp;nbsp;&lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;i&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&amp;quot;.&amp;nbsp;&lt;/i&gt;An aggregate always has a&amp;nbsp;&lt;i&gt;root&lt;/i&gt;.&lt;i&gt;&amp;nbsp;&lt;/i&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/LazyLoadingEagerLoadingAggDomain.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The order entity is the&amp;nbsp;&lt;i&gt;root&lt;/i&gt;&amp;nbsp;and the order lines belong to the aggregate (can be regarded as children of the&amp;nbsp;&lt;i&gt;root&lt;/i&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the&amp;nbsp;&lt;i&gt;order management&lt;/i&gt;&amp;nbsp;use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using&amp;nbsp;&lt;b&gt;eager loading&lt;/b&gt;&amp;nbsp;techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/7.aspx</link><pubDate>Wed, 30 Mar 2011 11:17:43 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:490</guid><dc:creator>markweee</dc:creator><description>Revision 7 posted to How to by markweee on 30/03/2011 08:17:43 a.m.&lt;br /&gt;
&lt;h2&gt;Lazy loading - eager loading&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: query, lazy loading&lt;/div&gt;

&lt;p&gt;&lt;span&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this article I want to discuss the&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt;&amp;nbsp;mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them. &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt;
&lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In my previous posts I showed&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles. &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/LazyLoadingEagerLoadingDomain.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the&amp;nbsp;&lt;b&gt;Build Action&lt;/b&gt;&amp;nbsp;of each mapping file to&amp;nbsp;&lt;b&gt;Embedded Resource&lt;/b&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;b&gt;hbm&lt;/b&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the&amp;nbsp;&lt;b&gt;Order&lt;/b&gt;&amp;nbsp;entity might be implemented as follows&lt;/p&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;set &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; cascade&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;key &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;one-to-many &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;set&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;p&gt;Analogous you can implement the mappings for the&amp;nbsp;&lt;b&gt;Customer&lt;/b&gt;&amp;nbsp;entity&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:3acfe276-b736-4c58-88ae-7c262976b50f" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and finally the mapping for the&amp;nbsp;&lt;b&gt;OrderLine&lt;/b&gt;&amp;nbsp;entity.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:23053d0d-5f88-44d1-8099-ff8406d62f4e" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:370px;border:1px solid gray;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt;&amp;nbsp;previous post for further details about the configuration file.
&lt;/p&gt;
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt;&amp;nbsp;principle) we implement the following base class.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:866px;background-color:#f4f4f4;max-height:866px;border:1px solid gray;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [SetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            After_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;b&gt;TestFixtureSetUp&lt;/b&gt;). When the whole test fixture is ended the session factory is closed (&lt;b&gt;TestFixtureTearDown&lt;/b&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual&amp;nbsp;&lt;b&gt;Before_each_test&lt;/b&gt;&amp;nbsp;method is called. After each test in the fixture is finished the virtual&amp;nbsp;&lt;b&gt;After_each_test&lt;/b&gt;&amp;nbsp;method is called. The two virtual methods can (but must not necessarily) be overridden in a child class.&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class&amp;nbsp;&lt;b&gt;Order_Fixture&lt;/b&gt;&amp;nbsp;to our test project and inherit from the&amp;nbsp;&lt;b&gt;TestFixtureBase&lt;/b&gt;&amp;nbsp;base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border:1px solid gray;cursor:text;margin:20px 0px 10px;max-height:749px;overflow:auto;padding:4px;width:97.5%;"&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;"&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;{&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;    {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#008000;"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            var customer = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span style="color:#006080;"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            var line1 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            var line2 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            _order = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                            OrderNumber = &lt;span style="color:#006080;"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                        };&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;                }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;    }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas,&amp;#39;Courier New&amp;#39;,Courier,Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px;width:100%;margin:0em;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The&amp;nbsp;&lt;b&gt;CreateInitialData&lt;/b&gt;&amp;nbsp;method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:170px;background-color:#f4f4f4;max-height:200px;border:1px solid gray;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
 
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:109px;background-color:#f4f4f4;max-height:200px;border:1px solid gray;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
    order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
    order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
    order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:202px;background-color:#f4f4f4;max-height:200px;border:1px solid gray;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
[ExpectedException(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(LazyInitializationException))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
    
    &lt;span style="color:#008000;"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// Note: at this point the session is already closed&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test&amp;nbsp;&lt;b&gt;only&lt;/b&gt;&amp;nbsp;succeeds if the method throws the expected exception of type&amp;nbsp;&lt;b&gt;LazyInitializationException&lt;/b&gt;. And this is just what we want to show!&lt;/p&gt;
&lt;p&gt;Another problem is the&amp;nbsp;&lt;b&gt;n+1 select statements problem&lt;/b&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:233px;background-color:#f4f4f4;max-height:200px;border:1px solid gray;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()
{
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
    {
        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
        &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; sum = 0;
        &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var line &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb.OrderLines)
        {
            &lt;span style="color:#008000;"&gt;// just some dummy code to force loading of order line&lt;/span&gt;
            sum += line.Amount;
        } 
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:235px;background-color:#f4f4f4;max-height:200px;border:1px solid gray;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id3_0_, 
       order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_3_0_, 
       order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate3_0_, 
       order0_.Customer &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Customer3_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;
 
&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; orderlines0_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id4_0_, 
       orderlines0_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount4_0_, 
       orderlines0_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_4_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   OrderLine orderlines0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  orderlines0_.OrderId=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the&amp;nbsp;&lt;b&gt;NHibernateUtil&lt;/b&gt;&amp;nbsp;class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:444px;background-color:#f4f4f4;max-height:444px;border:1px solid gray;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:286px;background-color:#f4f4f4;max-height:286px;border:1px solid gray;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; sql = &lt;span style="color:#006080;"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                        .SetGuid(&lt;span style="color:#006080;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:261px;background-color:#f4f4f4;max-height:260px;border:1px solid gray;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;      order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_1_, 
            customer2_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id2_2_, 
            order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
            order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
            order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_, 
            orderlines1_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount1_1_, 
            orderlines1_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_1_1_, 
            customer2_.CompanyName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CompanyN2_2_2_, 
            orderlines1_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId0__, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0__ 
&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;        Orders order0_ 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id 
&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;       order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely&amp;nbsp;&lt;b&gt;Orders&lt;/b&gt;,&amp;nbsp;&lt;b&gt;Customer&lt;/b&gt;&amp;nbsp;and&lt;b&gt;OrderLine&lt;/b&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of&amp;nbsp;&lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;i&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&amp;quot;.&amp;nbsp;&lt;/i&gt;An aggregate always has a&amp;nbsp;&lt;i&gt;root&lt;/i&gt;.&lt;i&gt;&amp;nbsp;&lt;/i&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/LazyLoadingEagerLoadingAggDomain.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The order entity is the&amp;nbsp;&lt;i&gt;root&lt;/i&gt;&amp;nbsp;and the order lines belong to the aggregate (can be regarded as children of the&amp;nbsp;&lt;i&gt;root&lt;/i&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the&amp;nbsp;&lt;i&gt;order management&lt;/i&gt;&amp;nbsp;use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using&amp;nbsp;&lt;b&gt;eager loading&lt;/b&gt;&amp;nbsp;techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/6.aspx</link><pubDate>Wed, 10 Sep 2008 14:32:59 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:372</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 6 posted to How to by Fabio Maulo on 10/09/2008 11:32:59 a.m.&lt;br /&gt;
&lt;h2&gt;Lazy loading - eager loading&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;loading&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&lt;span&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this article I want to discuss the&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt;&amp;nbsp;mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them.&lt;/p&gt;
&lt;p&gt;In my previous posts I showed&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles.&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="480" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" alt="image" height="389" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/LazyLoadingEagerLoadingDomain.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the&amp;nbsp;&lt;strong&gt;Build Action&lt;/strong&gt;&amp;nbsp;of each mapping file to&amp;nbsp;&lt;strong&gt;Embedded Resource&lt;/strong&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;strong&gt;hbm&lt;/strong&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the&amp;nbsp;&lt;strong&gt;Order&lt;/strong&gt;&amp;nbsp;entity might be implemented as follows&lt;/p&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;set &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; cascade&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;key &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;one-to-many &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;set&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;p&gt;Analogous you can implement the mappings for the&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;entity&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:3acfe276-b736-4c58-88ae-7c262976b50f" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and finally the mapping for the&amp;nbsp;&lt;strong&gt;OrderLine&lt;/strong&gt;&amp;nbsp;entity.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:23053d0d-5f88-44d1-8099-ff8406d62f4e" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:370px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt;&amp;nbsp;previous post for further details about the configuration file.
&lt;/p&gt;
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt;&amp;nbsp;principle) we implement the following base class.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:866px;background-color:#f4f4f4;max-height:866px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [SetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            After_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;strong&gt;TestFixtureSetUp&lt;/strong&gt;). When the whole test fixture is ended the session factory is closed (&lt;strong&gt;TestFixtureTearDown&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual&amp;nbsp;&lt;strong&gt;Before_each_test&lt;/strong&gt;&amp;nbsp;method is called. After each test in the fixture is finished the virtual&amp;nbsp;&lt;strong&gt;After_each_test&lt;/strong&gt;&amp;nbsp;method is called. The two virtual methods can (but must not necessarily) be overridden in a child class.&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class&amp;nbsp;&lt;strong&gt;Order_Fixture&lt;/strong&gt;&amp;nbsp;to our test project and inherit from the&amp;nbsp;&lt;strong&gt;TestFixtureBase&lt;/strong&gt;&amp;nbsp;base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border:solid 1px gray;cursor:text;margin:20px 0px 10px 0px;max-height:749px;overflow:auto;padding:4px 4px 4px 4px;width:97.5%;"&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;"&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;{&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#008000;"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var customer = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span style="color:#006080;"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var line1 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var line2 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            OrderNumber = &lt;span style="color:#006080;"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                        };&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The&amp;nbsp;&lt;strong&gt;CreateInitialData&lt;/strong&gt;&amp;nbsp;method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:170px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
 
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:109px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
    order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
    order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
    order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:202px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
[ExpectedException(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(LazyInitializationException))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
    
    &lt;span style="color:#008000;"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// Note: at this point the session is already closed&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test&amp;nbsp;&lt;strong&gt;only&lt;/strong&gt;&amp;nbsp;succeeds if the method throws the expected exception of type&amp;nbsp;&lt;strong&gt;LazyInitializationException&lt;/strong&gt;. And this is just what we want to show!&lt;/p&gt;
&lt;p&gt;Another problem is the&amp;nbsp;&lt;strong&gt;n+1 select statements problem&lt;/strong&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:233px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()
{
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
    {
        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
        &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; sum = 0;
        &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var line &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb.OrderLines)
        {
            &lt;span style="color:#008000;"&gt;// just some dummy code to force loading of order line&lt;/span&gt;
            sum += line.Amount;
        } 
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:235px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id3_0_, 
       order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_3_0_, 
       order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate3_0_, 
       order0_.Customer &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Customer3_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;
 
&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; orderlines0_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id4_0_, 
       orderlines0_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount4_0_, 
       orderlines0_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_4_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   OrderLine orderlines0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  orderlines0_.OrderId=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the&amp;nbsp;&lt;strong&gt;NHibernateUtil&lt;/strong&gt;&amp;nbsp;class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:444px;background-color:#f4f4f4;max-height:444px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:286px;background-color:#f4f4f4;max-height:286px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; sql = &lt;span style="color:#006080;"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                        .SetGuid(&lt;span style="color:#006080;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:261px;background-color:#f4f4f4;max-height:260px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;      order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_1_, 
            customer2_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id2_2_, 
            order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
            order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
            order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_, 
            orderlines1_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount1_1_, 
            orderlines1_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_1_1_, 
            customer2_.CompanyName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CompanyN2_2_2_, 
            orderlines1_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId0__, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0__ 
&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;        Orders order0_ 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id 
&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;       order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely&amp;nbsp;&lt;strong&gt;Orders&lt;/strong&gt;,&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;and&lt;strong&gt;OrderLine&lt;/strong&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of&amp;nbsp;&lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;em&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&amp;quot;.&amp;nbsp;&lt;/em&gt;An aggregate always has a&amp;nbsp;&lt;em&gt;root&lt;/em&gt;.&lt;em&gt;&amp;nbsp;&lt;/em&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="575" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" alt="image" height="445" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/LazyLoadingEagerLoadingAggDomain.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The order entity is the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;&amp;nbsp;and the order lines belong to the aggregate (can be regarded as children of the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the&amp;nbsp;&lt;em&gt;order management&lt;/em&gt;&amp;nbsp;use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using&amp;nbsp;&lt;strong&gt;eager loading&lt;/strong&gt;&amp;nbsp;techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/5.aspx</link><pubDate>Sun, 07 Sep 2008 04:34:42 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:96</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 5 posted to How to by Fabio Maulo on 07/09/2008 01:34:42 a.m.&lt;br /&gt;
&lt;h2&gt;Lazy loading - eager loading&lt;/h2&gt;
&lt;p&gt;&lt;span&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img height="21" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" width="244" border="0" /&gt;&lt;/span&gt;&lt;p&gt;&lt;span&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this article I want to discuss the&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt;&amp;nbsp;mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them.&lt;/p&gt;
&lt;p&gt;In my previous posts I showed&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles.&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img height="389" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" width="480" border="0" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="480" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" alt="image" height="389" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the&amp;nbsp;&lt;strong&gt;Build Action&lt;/strong&gt;&amp;nbsp;of each mapping file to&amp;nbsp;&lt;strong&gt;Embedded Resource&lt;/strong&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;strong&gt;hbm&lt;/strong&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the&amp;nbsp;&lt;strong&gt;Order&lt;/strong&gt;&amp;nbsp;entity might be implemented as follows&lt;/p&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;set &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; cascade&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;key &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;one-to-many &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;set&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;p&gt;Analogous you can implement the mappings for the&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;entity&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:3acfe276-b736-4c58-88ae-7c262976b50f" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and finally the mapping for the&amp;nbsp;&lt;strong&gt;OrderLine&lt;/strong&gt;&amp;nbsp;entity.&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:23053d0d-5f88-44d1-8099-ff8406d62f4e" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:370px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt;&amp;nbsp;previous post for further details about the configuration file.
&lt;/p&gt;
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt;&amp;nbsp;principle) we implement the following base class.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:866px;background-color:#f4f4f4;max-height:866px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [SetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            After_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;strong&gt;TestFixtureSetUp&lt;/strong&gt;). When the whole test fixture is ended the session factory is closed (&lt;strong&gt;TestFixtureTearDown&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual&amp;nbsp;&lt;strong&gt;Before_each_test&lt;/strong&gt;&amp;nbsp;method is called. After each test in the fixture is finished the virtual&amp;nbsp;&lt;strong&gt;After_each_test&lt;/strong&gt;&amp;nbsp;method is called. The two virtual methods can (but must not necessarily) be overridden in a child class.&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class&amp;nbsp;&lt;strong&gt;Order_Fixture&lt;/strong&gt;&amp;nbsp;to our test project and inherit from the&amp;nbsp;&lt;strong&gt;TestFixtureBase&lt;/strong&gt;&amp;nbsp;base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border:solid 1px gray;cursor:text;margin:20px 0px 10px 0px;max-height:749px;overflow:auto;padding:4px 4px 4px 4px;width:97.5%;"&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;"&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;{&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#008000;"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var customer = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span style="color:#006080;"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var line1 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var line2 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            OrderNumber = &lt;span style="color:#006080;"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                        };&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The&amp;nbsp;&lt;strong&gt;CreateInitialData&lt;/strong&gt;&amp;nbsp;method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:170px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
 
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:109px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
    order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
    order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
    order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:202px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
[ExpectedException(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(LazyInitializationException))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
    
    &lt;span style="color:#008000;"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// Note: at this point the session is already closed&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test&amp;nbsp;&lt;strong&gt;only&lt;/strong&gt;&amp;nbsp;succeeds if the method throws the expected exception of type&amp;nbsp;&lt;strong&gt;LazyInitializationException&lt;/strong&gt;. And this is just what we want to show!&lt;/p&gt;
&lt;p&gt;Another problem is the&amp;nbsp;&lt;strong&gt;n+1 select statements problem&lt;/strong&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:233px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()
{
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
    {
        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
        &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; sum = 0;
        &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var line &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb.OrderLines)
        {
            &lt;span style="color:#008000;"&gt;// just some dummy code to force loading of order line&lt;/span&gt;
            sum += line.Amount;
        } 
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:235px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id3_0_, 
       order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_3_0_, 
       order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate3_0_, 
       order0_.Customer &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Customer3_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;
 
&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; orderlines0_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id4_0_, 
       orderlines0_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount4_0_, 
       orderlines0_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_4_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   OrderLine orderlines0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  orderlines0_.OrderId=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the&amp;nbsp;&lt;strong&gt;NHibernateUtil&lt;/strong&gt;&amp;nbsp;class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:444px;background-color:#f4f4f4;max-height:444px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:286px;background-color:#f4f4f4;max-height:286px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; sql = &lt;span style="color:#006080;"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                        .SetGuid(&lt;span style="color:#006080;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:261px;background-color:#f4f4f4;max-height:260px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;      order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_1_, 
            customer2_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id2_2_, 
            order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
            order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
            order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_, 
            orderlines1_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount1_1_, 
            orderlines1_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_1_1_, 
            customer2_.CompanyName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CompanyN2_2_2_, 
            orderlines1_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId0__, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0__ 
&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;        Orders order0_ 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id 
&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;       order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely&amp;nbsp;&lt;strong&gt;Orders&lt;/strong&gt;,&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;and&lt;strong&gt;OrderLine&lt;/strong&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of&amp;nbsp;&lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;em&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&amp;quot;.&amp;nbsp;&lt;/em&gt;An aggregate always has a&amp;nbsp;&lt;em&gt;root&lt;/em&gt;.&lt;em&gt;&amp;nbsp;&lt;/em&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img height="445" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" width="575" border="0" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="575" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" alt="image" height="445" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The order entity is the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;&amp;nbsp;and the order lines belong to the aggregate (can be regarded as children of the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the&amp;nbsp;&lt;em&gt;order management&lt;/em&gt;&amp;nbsp;use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using&amp;nbsp;&lt;strong&gt;eager loading&lt;/strong&gt;&amp;nbsp;techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img height="21" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" width="244" border="0" /&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/4.aspx</link><pubDate>Sun, 07 Sep 2008 04:33:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:50</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 4 posted to How to by Fabio Maulo on 07/09/2008 01:33:15 a.m.&lt;br /&gt;
&lt;h2&gt;Lazy loading - eager loading&lt;/h2&gt;
&lt;p&gt;&lt;span&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="244" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" alt="Blog Signature Gabriel" height="21" /&gt;&lt;/span&gt;&lt;p&gt;&lt;span&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img height="21" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" width="244" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this article I want to discuss the&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt;&amp;nbsp;mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them.&lt;/p&gt;
&lt;p&gt;In my previous posts I showed&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles.&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="480" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" alt="image" height="389" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img height="389" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" width="480" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the&amp;nbsp;&lt;strong&gt;Build Action&lt;/strong&gt;&amp;nbsp;of each mapping file to&amp;nbsp;&lt;strong&gt;Embedded Resource&lt;/strong&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;strong&gt;hbm&lt;/strong&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the&amp;nbsp;&lt;strong&gt;Order&lt;/strong&gt;&amp;nbsp;entity might be implemented as follows&lt;/p&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml &lt;/span&gt; &lt;span class="attr"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;version=&lt;/span&gt;&amp;quot;1.0&amp;quot; &lt;/span&gt; &lt;span class="attr"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;encoding=&lt;/span&gt;&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;set &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; cascade&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;key &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;one-to-many &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;set&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;p&gt;Analogous you can implement the mappings for the&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;entity&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:3acfe276-b736-4c58-88ae-7c262976b50f" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml &lt;/span&gt; &lt;span class="attr"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;version=&lt;/span&gt;&amp;quot;1.0&amp;quot; &lt;/span&gt; &lt;span class="attr"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;encoding=&lt;/span&gt;&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and finally the mapping for the&amp;nbsp;&lt;strong&gt;OrderLine&lt;/strong&gt;&amp;nbsp;entity.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:23053d0d-5f88-44d1-8099-ff8406d62f4e" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml &lt;/span&gt; &lt;span class="attr"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;version=&lt;/span&gt;&amp;quot;1.0&amp;quot; &lt;/span&gt; &lt;span class="attr"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span class="kwrd"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;encoding=&lt;/span&gt;&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; 
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
 
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:370px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt;&amp;nbsp;previous post for further details about the configuration file.
&lt;/p&gt;
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt;&amp;nbsp;principle) we implement the following base class.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:866px;background-color:#f4f4f4;max-height:866px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [SetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            After_each_test();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        { }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;strong&gt;TestFixtureSetUp&lt;/strong&gt;). When the whole test fixture is ended the session factory is closed (&lt;strong&gt;TestFixtureTearDown&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual&amp;nbsp;&lt;strong&gt;Before_each_test&lt;/strong&gt;&amp;nbsp;method is called. After each test in the fixture is finished the virtual&amp;nbsp;&lt;strong&gt;After_each_test&lt;/strong&gt;&amp;nbsp;method is called. The two virtual methods can (but must not necessarily) be overridden in a child class.&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class&amp;nbsp;&lt;strong&gt;Order_Fixture&lt;/strong&gt;&amp;nbsp;to our test project and inherit from the&amp;nbsp;&lt;strong&gt;TestFixtureBase&lt;/strong&gt;&amp;nbsp;base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border:solid 1px gray;cursor:text;margin:20px 0px 10px 0px;max-height:749px;overflow:auto;padding:4px 4px 4px 4px;width:97.5%;"&gt;
&lt;div style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;"&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;{&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#008000;"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var customer = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span style="color:#006080;"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var line1 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            var line2 = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span style="color:#006080;"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                        {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            OrderNumber = &lt;span style="color:#006080;"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                        };&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt; &lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                {&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;                }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;        }&lt;/pre&gt;
&lt;pre style="background-color:white;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;    }&lt;/pre&gt;
&lt;pre style="background-color:#f4f4f4;font-family:Consolas, &amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:8pt;line-height:12pt;border-style:none;color:black;overflow:visible;padding:0px 0px 0px 0px;width:100%;margin:0em;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The&amp;nbsp;&lt;strong&gt;CreateInitialData&lt;/strong&gt;&amp;nbsp;method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:170px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
 
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));
    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:109px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
    order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
    order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
    order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:202px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
[ExpectedException(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(LazyInitializationException))]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()
{
    Order fromDb;
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
    
    &lt;span style="color:#008000;"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;
    &lt;span style="color:#008000;"&gt;// Note: at this point the session is already closed&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test&amp;nbsp;&lt;strong&gt;only&lt;/strong&gt;&amp;nbsp;succeeds if the method throws the expected exception of type&amp;nbsp;&lt;strong&gt;LazyInitializationException&lt;/strong&gt;. And this is just what we want to show!&lt;/p&gt;
&lt;p&gt;Another problem is the&amp;nbsp;&lt;strong&gt;n+1 select statements problem&lt;/strong&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:233px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[Test]
&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()
{
    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())
    {
        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);
        &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; sum = 0;
        &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var line &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb.OrderLines)
        {
            &lt;span style="color:#008000;"&gt;// just some dummy code to force loading of order line&lt;/span&gt;
            sum += line.Amount;
        } 
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:235px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id3_0_, 
       order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_3_0_, 
       order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate3_0_, 
       order0_.Customer &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Customer3_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Orders order0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;
 
&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; orderlines0_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_, 
       orderlines0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id4_0_, 
       orderlines0_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount4_0_, 
       orderlines0_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_4_0_ 
&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   OrderLine orderlines0_ 
&lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  orderlines0_.OrderId=&lt;span style="color:#006080;"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the&amp;nbsp;&lt;strong&gt;NHibernateUtil&lt;/strong&gt;&amp;nbsp;class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:444px;background-color:#f4f4f4;max-height:444px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:286px;background-color:#f4f4f4;max-height:286px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; sql = &lt;span style="color:#006080;"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#006080;"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                        .SetGuid(&lt;span style="color:#006080;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:261px;background-color:#f4f4f4;max-height:260px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;      order0_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0_0_, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id1_1_, 
            customer2_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id2_2_, 
            order0_.OrderNumber &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
            order0_.OrderDate &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderDate0_0_, 
            order0_.CustomerId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CustomerId0_0_, 
            orderlines1_.Amount &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Amount1_1_, 
            orderlines1_.ProductName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; ProductN3_1_1_, 
            customer2_.CompanyName &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; CompanyN2_2_2_, 
            orderlines1_.OrderId &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; OrderId0__, 
            orderlines1_.Id &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; Id0__ 
&lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;        Orders order0_ 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId 
&lt;span style="color:#0000ff;"&gt;inner&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id 
&lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;       order0_.Id=&lt;span style="color:#006080;"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely&amp;nbsp;&lt;strong&gt;Orders&lt;/strong&gt;,&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;and&lt;strong&gt;OrderLine&lt;/strong&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of&amp;nbsp;&lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;em&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&amp;quot;.&amp;nbsp;&lt;/em&gt;An aggregate always has a&amp;nbsp;&lt;em&gt;root&lt;/em&gt;.&lt;em&gt;&amp;nbsp;&lt;/em&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="575" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" alt="image" height="445" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img height="445" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" width="575" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The order entity is the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;&amp;nbsp;and the order lines belong to the aggregate (can be regarded as children of the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the&amp;nbsp;&lt;em&gt;order management&lt;/em&gt;&amp;nbsp;use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using&amp;nbsp;&lt;strong&gt;eager loading&lt;/strong&gt;&amp;nbsp;techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="244" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" alt="Blog Signature Gabriel" height="21" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img height="21" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" width="244" border="0" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/3.aspx</link><pubDate>Sat, 06 Sep 2008 22:02:06 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:49</guid><dc:creator>gabriel.schenker</dc:creator><description>Revision 3 posted to How to by gabriel.schenker on 06/09/2008 07:02:06 p.m.&lt;br /&gt;
&lt;h2&gt;Lazy loading - eager loading&lt;/h2&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="244" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" alt="Blog Signature Gabriel" height="21" style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" /&gt;&lt;/span&gt;&lt;p&gt;&lt;span&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="244" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" alt="Blog Signature Gabriel" height="21" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this article I want to discuss the&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt;&amp;nbsp;mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them.&lt;/p&gt;
&lt;p&gt;In my previous posts I showed&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="http://www.nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles.&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="480" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" alt="image" height="389" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="480" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" alt="image" height="389" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the&amp;nbsp;&lt;strong&gt;Build Action&lt;/strong&gt;&amp;nbsp;of each mapping file to&amp;nbsp;&lt;strong&gt;Embedded Resource&lt;/strong&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;strong&gt;hbm&lt;/strong&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the&amp;nbsp;&lt;strong&gt;Order&lt;/strong&gt;&amp;nbsp;entity might be implemented as follows&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;table&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;many-to-one&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;set&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;cascade&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;key&lt;/span&gt; &lt;span class="attr"&gt;column&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;one-to-many&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;set&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Analogous you can implement the mappings for the&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;entity&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and finally the mapping for the&amp;nbsp;&lt;strong&gt;OrderLine&lt;/strong&gt;&amp;nbsp;entity.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        [Test]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            var cfg = &lt;span class="kwrd"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre class="alt"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre&gt;            cfg.AddAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;)&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;;&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;    }&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult&amp;nbsp;&lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt;&amp;nbsp;previous post for further details about the configuration file.
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt;&amp;nbsp;principle) we implement the following base class.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            _configuration = &lt;span class="kwrd"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre class="alt"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre&gt;            _configuration.AddAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre class="alt"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        [SetUp]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            After_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre&gt;        { }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre class="alt"&gt;        { }&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;strong&gt;TestFixtureSetUp&lt;/strong&gt;). When the whole test fixture is ended the session factory is closed (&lt;strong&gt;TestFixtureTearDown&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual&amp;nbsp;&lt;strong&gt;Before_each_test&lt;/strong&gt;&amp;nbsp;method is called. After each test in the fixture is finished the virtual&amp;nbsp;&lt;strong&gt;After_each_test&lt;/strong&gt;&amp;nbsp;method is called. The two virtual methods can (but must not necessarily) be overridden in a child class&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
.&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class&amp;nbsp;&lt;strong&gt;Order_Fixture&lt;/strong&gt;&amp;nbsp;to our test project and inherit from the&amp;nbsp;&lt;strong&gt;TestFixtureBase&lt;/strong&gt;&amp;nbsp;base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="rem"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;            var customer = &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span class="str"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;            var line1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span class="str"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre class="alt"&gt;            var line2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span class="str"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;            _order = &lt;span class="kwrd"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre class="alt"&gt;                        {&lt;/pre&gt;
&lt;pre&gt;                            OrderNumber = &lt;span class="str"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre class="alt"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre class="alt"&gt;                        };&lt;/pre&gt;
&lt;pre&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre class="alt"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;                &lt;span class="kwrd"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre class="alt"&gt;                {&lt;/pre&gt;
&lt;pre&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre class="alt"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre&gt;                    transaction.Commit()&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;                }&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;        }&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre&gt;}&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The&amp;nbsp;&lt;strong&gt;CreateInitialData&lt;/strong&gt;&amp;nbsp;method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines))&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;}&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0_0_, &lt;/pre&gt;
&lt;pre&gt;    order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_0_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;    order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate0_0_, &lt;/pre&gt;
&lt;pre&gt;    order0_.CustomerId &lt;span class="kwrd"&gt;as&lt;/span&gt; CustomerId0_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;FROM&lt;/span&gt;    Orders order0_ &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span class="str"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;[ExpectedException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(LazyInitializationException))]&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;    &lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="rem"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="rem"&gt;// Note: at this point the session is already closed&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test&amp;nbsp;&lt;strong&gt;only&lt;/strong&gt;&amp;nbsp;succeeds if the method throws the expected exception of type&amp;nbsp;&lt;strong&gt;LazyInitializationException&lt;/strong&gt;. And this is just what we want to show!&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;Another problem is the&amp;nbsp;&lt;strong&gt;n+1 select statements problem&lt;/strong&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;int&lt;/span&gt; sum = 0;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var line &lt;span class="kwrd"&gt;in&lt;/span&gt; fromDb.OrderLines)&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="rem"&gt;// just some dummy code to force loading of order line&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;            sum += line.Amount;&lt;/pre&gt;
&lt;pre&gt;        } &lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;SELECT order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id3_0_, &lt;/pre&gt;
&lt;pre&gt;       order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_3_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate3_0_, &lt;/pre&gt;
&lt;pre&gt;       order0_.Customer &lt;span class="kwrd"&gt;as&lt;/span&gt; Customer3_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;FROM   Orders order0_ &lt;/pre&gt;
&lt;pre&gt;WHERE  order0_.Id=&lt;span class="str"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;SELECT orderlines0_.OrderId &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderId1_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       orderlines0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id1_, &lt;/pre&gt;
&lt;pre&gt;       orderlines0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id4_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       orderlines0_.Amount &lt;span class="kwrd"&gt;as&lt;/span&gt; Amount4_0_, &lt;/pre&gt;
&lt;pre&gt;       orderlines0_.ProductName &lt;span class="kwrd"&gt;as&lt;/span&gt; ProductN3_4_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;FROM   OrderLine orderlines0_ &lt;/pre&gt;
&lt;pre&gt;WHERE  orderlines0_.OrderId=&lt;span class="str"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the&amp;nbsp;&lt;strong&gt;NHibernateUtil&lt;/strong&gt;&amp;nbsp;class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines))&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;;&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;string&lt;/span&gt; sql = &lt;span class="str"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre&gt;                     &lt;span class="str"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre class="alt"&gt;                     &lt;span class="str"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre&gt;                     &lt;span class="str"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre&gt;                        .SetGuid(&lt;span class="str"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre class="alt"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines))&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;;&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt;      order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0_0_, 
            orderlines1_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id1_1_, 
            customer2_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id2_2_, 
            order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
            order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate0_0_, 
            order0_.CustomerId &lt;span class="kwrd"&gt;as&lt;/span&gt; CustomerId0_0_, 
            orderlines1_.Amount &lt;span class="kwrd"&gt;as&lt;/span&gt; Amount1_1_, 
            orderlines1_.ProductName &lt;span class="kwrd"&gt;as&lt;/span&gt; ProductN3_1_1_, 
            customer2_.CompanyName &lt;span class="kwrd"&gt;as&lt;/span&gt; CompanyN2_2_2_, 
            orderlines1_.OrderId &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderId0__, 
            orderlines1_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0__ 
&lt;span class="kwrd"&gt;from&lt;/span&gt;        Orders order0_ 
&lt;span class="kwrd"&gt;inner&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span class="kwrd"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId 
&lt;span class="kwrd"&gt;inner&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span class="kwrd"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id 
&lt;span class="kwrd"&gt;where&lt;/span&gt;       order0_.Id=&lt;span class="str"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-size&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;small&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;black&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;font-family&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consolas&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Courier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;New&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;courier&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;monospace&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffffff;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*white-space:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pre&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;008000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;kwrd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000ff;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;str&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;006080;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;op&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;0000c0;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;preproc&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;cc6633;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;asp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ffff00;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;html&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;800000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;attr&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ff0000;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;alt&lt;/span&gt; 
&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;f4f4f4;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;width&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;100%&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
	&lt;span style="text-decoration: line-through; color: red;"&gt;margin&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;0em&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;csharpcode&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lnum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;color&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;606060;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely&amp;nbsp;&lt;strong&gt;Orders&lt;/strong&gt;,&amp;nbsp;&lt;strong&gt;Customer&lt;/strong&gt;&amp;nbsp;and&lt;strong&gt;OrderLine&lt;/strong&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of&amp;nbsp;&lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;em&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;.&amp;nbsp;&lt;/span&gt;&lt;/em&gt;An aggregate always has a&amp;nbsp;&lt;em&gt;root&lt;/em&gt;.&lt;em&gt;&amp;nbsp;&lt;/em&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="575" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" alt="image" height="445" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="575" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" alt="image" height="445" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The order entity is the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;&amp;nbsp;and the order lines belong to the aggregate (can be regarded as children of the&amp;nbsp;&lt;em&gt;root&lt;/em&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the&amp;nbsp;&lt;em&gt;order management&lt;/em&gt;&amp;nbsp;use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using&amp;nbsp;&lt;strong&gt;eager loading&lt;/strong&gt;&amp;nbsp;techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img border="0" width="244" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" alt="Blog Signature Gabriel" height="21" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="244" src="http://www.nhforge.org/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" alt="Blog Signature Gabriel" height="21" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/2.aspx</link><pubDate>Sat, 06 Sep 2008 21:55:57 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:48</guid><dc:creator>gabriel.schenker</dc:creator><description>Revision 2 posted to How to by gabriel.schenker on 06/09/2008 06:55:57 p.m.&lt;br /&gt;
&lt;h2&gt;Lazy loading - eager loading&lt;/h2&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" width="244" border="0" height="21" /&gt;&lt;/span&gt;&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="244" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" alt="Blog Signature Gabriel" height="21" style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" /&gt;&lt;/span&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;In this article I want to discuss the &lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt; mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them.&lt;/p&gt;
&lt;p&gt;In my previous posts I showed &lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt; and &lt;a href="/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles.&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" width="480" border="0" height="389" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="480" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" alt="image" height="389" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" /&gt;&lt;/span&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the &lt;strong&gt;Build Action&lt;/strong&gt; of each mapping file to &lt;strong&gt;Embedded Resource&lt;/strong&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;strong&gt;hbm&lt;/strong&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the &lt;strong&gt;Order&lt;/strong&gt; entity might be implemented as follows&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;table&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;many-to-one&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;set&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;cascade&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;key&lt;/span&gt; &lt;span class="attr"&gt;column&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;one-to-many&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;set&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;Analogous you can implement the mappings for the &lt;strong&gt;Customer&lt;/strong&gt; entity&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;and finally the mapping for the &lt;strong&gt;OrderLine&lt;/strong&gt; entity.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        [Test]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            var cfg = &lt;span class="kwrd"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre class="alt"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre&gt;            cfg.AddAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult &lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt; previous post for further details about the configuration file. 
&lt;/p&gt;
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt; principle) we implement the following base class.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            _configuration = &lt;span class="kwrd"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre class="alt"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre&gt;            _configuration.AddAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre class="alt"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        [SetUp]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            After_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre&gt;        { }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre class="alt"&gt;        { }&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;strong&gt;TestFixtureSetUp&lt;/strong&gt;). When the whole test fixture is ended the session factory is closed (&lt;strong&gt;TestFixtureTearDown&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual &lt;strong&gt;Before_each_test&lt;/strong&gt; method is called. After each test in the fixture is finished the virtual &lt;strong&gt;After_each_test&lt;/strong&gt; method is called. The two virtual methods can (but must not necessarily) be overridden in a child class.
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class &lt;strong&gt;Order_Fixture&lt;/strong&gt; to our test project and inherit from the &lt;strong&gt;TestFixtureBase&lt;/strong&gt; base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="rem"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;            var customer = &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span class="str"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;            var line1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span class="str"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre class="alt"&gt;            var line2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span class="str"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;            _order = &lt;span class="kwrd"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre class="alt"&gt;                        {&lt;/pre&gt;
&lt;pre&gt;                            OrderNumber = &lt;span class="str"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre class="alt"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre class="alt"&gt;                        };&lt;/pre&gt;
&lt;pre&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre class="alt"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;                &lt;span class="kwrd"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre class="alt"&gt;                {&lt;/pre&gt;
&lt;pre&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre class="alt"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre class="alt"&gt;                }&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;CreateInitialData&lt;/strong&gt; method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0_0_, &lt;/pre&gt;
&lt;pre&gt;    order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_0_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;    order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate0_0_, &lt;/pre&gt;
&lt;pre&gt;    order0_.CustomerId &lt;span class="kwrd"&gt;as&lt;/span&gt; CustomerId0_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;FROM&lt;/span&gt;    Orders order0_ &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span class="str"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;[ExpectedException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(LazyInitializationException))]&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;    &lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="rem"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="rem"&gt;// Note: at this point the session is already closed&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test &lt;strong&gt;only&lt;/strong&gt; succeeds if the method throws the expected exception of type &lt;strong&gt;LazyInitializationException&lt;/strong&gt;. And this is just what we want to show!
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;Another problem is the &lt;strong&gt;n+1 select statements problem&lt;/strong&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;int&lt;/span&gt; sum = 0;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var line &lt;span class="kwrd"&gt;in&lt;/span&gt; fromDb.OrderLines)&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="rem"&gt;// just some dummy code to force loading of order line&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;            sum += line.Amount;&lt;/pre&gt;
&lt;pre&gt;        } &lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;SELECT order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id3_0_, &lt;/pre&gt;
&lt;pre&gt;       order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_3_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate3_0_, &lt;/pre&gt;
&lt;pre&gt;       order0_.Customer &lt;span class="kwrd"&gt;as&lt;/span&gt; Customer3_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;FROM   Orders order0_ &lt;/pre&gt;
&lt;pre&gt;WHERE  order0_.Id=&lt;span class="str"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;SELECT orderlines0_.OrderId &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderId1_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       orderlines0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id1_, &lt;/pre&gt;
&lt;pre&gt;       orderlines0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id4_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       orderlines0_.Amount &lt;span class="kwrd"&gt;as&lt;/span&gt; Amount4_0_, &lt;/pre&gt;
&lt;pre&gt;       orderlines0_.ProductName &lt;span class="kwrd"&gt;as&lt;/span&gt; ProductN3_4_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;FROM   OrderLine orderlines0_ &lt;/pre&gt;
&lt;pre&gt;WHERE  orderlines0_.OrderId=&lt;span class="str"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the &lt;strong&gt;NHibernateUtil&lt;/strong&gt; class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;string&lt;/span&gt; sql = &lt;span class="str"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre&gt;                     &lt;span class="str"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre class="alt"&gt;                     &lt;span class="str"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre&gt;                     &lt;span class="str"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre&gt;                        .SetGuid(&lt;span class="str"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre class="alt"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt;      order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0_0_, 
            orderlines1_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id1_1_, 
            customer2_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id2_2_, 
            order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_0_0_, 
            order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate0_0_, 
            order0_.CustomerId &lt;span class="kwrd"&gt;as&lt;/span&gt; CustomerId0_0_, 
            orderlines1_.Amount &lt;span class="kwrd"&gt;as&lt;/span&gt; Amount1_1_, 
            orderlines1_.ProductName &lt;span class="kwrd"&gt;as&lt;/span&gt; ProductN3_1_1_, 
            customer2_.CompanyName &lt;span class="kwrd"&gt;as&lt;/span&gt; CompanyN2_2_2_, 
            orderlines1_.OrderId &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderId0__, 
            orderlines1_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0__ 
&lt;span class="kwrd"&gt;from&lt;/span&gt;        Orders order0_ 
&lt;span class="kwrd"&gt;inner&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span class="kwrd"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId 
&lt;span class="kwrd"&gt;inner&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span class="kwrd"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id 
&lt;span class="kwrd"&gt;where&lt;/span&gt;       order0_.Id=&lt;span class="str"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely &lt;strong&gt;Orders&lt;/strong&gt;, &lt;strong&gt;Customer&lt;/strong&gt; and &lt;strong&gt;OrderLine&lt;/strong&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of &lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;em&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&amp;quot;. &lt;/em&gt;An aggregate always has a &lt;em&gt;root&lt;/em&gt;.&lt;em&gt;&amp;nbsp;&lt;/em&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" width="575" border="0" height="445" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="575" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" alt="image" height="445" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" /&gt;&lt;/span&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The order entity is the &lt;em&gt;root&lt;/em&gt; and the order lines belong to the aggregate (can be regarded as children of the &lt;em&gt;root&lt;/em&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the &lt;em&gt;order management&lt;/em&gt; use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using &lt;strong&gt;eager loading&lt;/strong&gt; techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" width="244" border="0" height="21" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img border="0" width="244" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" alt="Blog Signature Gabriel" height="21" style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Lazy loading - eager loading</title><link>http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading/revision/1.aspx</link><pubDate>Sat, 06 Sep 2008 21:48:11 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:47</guid><dc:creator>gabriel.schenker</dc:creator><description>Revision 1 posted to How to by gabriel.schenker on 06/09/2008 06:48:11 p.m.&lt;br /&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_4.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb_5F00_1.png" width="244" border="0" height="21" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;In this article I want to discuss the &lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy loading&lt;/a&gt; mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let&amp;#39;s discuss these effects and how to avoid them.&lt;/p&gt;
&lt;p&gt;In my previous posts I showed &lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/03/31/prepare-your-system-for-nhibernate.aspx"&gt;how to prepare your system for NHibernate&lt;/a&gt; and &lt;a href="/wikis/howtonh/your-first-nhibernate-based-application.aspx"&gt;how to implement a first NHibernate base application&lt;/a&gt;. This post is based on those two articles.&lt;/p&gt;
&lt;h2&gt;The Domain&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s first define a simple domain. It shows part of an order entry system. I keep this model as simple as possible (a real domain model would be more complex) but it contains all aspects we want to discuss in this post. Below is the class diagram of our model&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_4.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_1.png" width="480" border="0" height="389" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;We have an order entity which can be placed by a customer entity. Each order can have many order line entities. Each of the three entity types is uniquely identified by a property Id (surrogate key).&lt;/p&gt;
&lt;h2&gt;The Mapping Files&lt;/h2&gt;
&lt;p&gt;We have to write one mapping file per entity. It is recommended that you always have one mapping per file. Don&amp;#39;t forget to set the &lt;b&gt;Build Action&lt;/b&gt; of each mapping file to &lt;b&gt;Embedded Resource&lt;/b&gt;. People often tend to forget it and the subsequent errors raised by NHibernate are not always obvious. Also do not forget to give the mapping files the correct name, that is *.&lt;b&gt;hbm&lt;/b&gt;.xml where * denotes the placeholder for the entity name.&lt;/p&gt;
&lt;p&gt;The mapping for the &lt;b&gt;Order&lt;/b&gt; entity might be implemented as follows&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Order&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;table&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Orders&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderNumber&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderDate&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;many-to-one&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;set&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLines&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;cascade&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;all-delete-orphan&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;key&lt;/span&gt; &lt;span class="attr"&gt;column&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderId&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;one-to-many&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;set&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;Analogous you can implement the mappings for the &lt;b&gt;Customer&lt;/b&gt; entity&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customer&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;CompanyName&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;and finally the mapping for the &lt;b&gt;OrderLine&lt;/b&gt; entity.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;                   &lt;span class="attr"&gt;assembly&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;                   &lt;span class="attr"&gt;namespace&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LazyLoadEagerLoad.Domain&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;OrderLine&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;generator&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Amount&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;property&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ProductName&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;  &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;hibernate-mapping&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Testing the Mapping&lt;/h2&gt;
&lt;p&gt;To test the mapping we use the following test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        [Test]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            var cfg = &lt;span class="kwrd"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre class="alt"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre&gt;            cfg.AddAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Order).Assembly);&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
First we create a new instance of the NHibernate Configuration class and tell it to configure itself. Since we don&amp;#39;t provide any explicit configuration here in the code NHibernate looks out for an adequate configuration file. I have included such a file (called hibernate.cfg.xml) in my project. Please consult &lt;a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx"&gt;this&lt;/a&gt; previous post for further details about the configuration file. 
&lt;/p&gt;
&lt;h2&gt;Testing the Loading Behavior of NHibernate&lt;/h2&gt;
&lt;h3&gt;Defining a base class for our tests&lt;/h3&gt;
&lt;p&gt;To avoid repetitive task (&lt;a href="http://en.wikipedia.org/wiki/Don&amp;#39;t_repeat_yourself"&gt;DRY&lt;/a&gt; principle) we implement the following base class.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TestFixtureBase&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _sessionFactory; }&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            _configuration = &lt;span class="kwrd"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre class="alt"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre&gt;            _configuration.AddAssembly(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Customer).Assembly);&lt;/pre&gt;
&lt;pre class="alt"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        [TestFixtureTearDown]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TestFixtureTearDown()&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;            _sessionFactory.Close();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        [SetUp]&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetupContext()&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;true&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;            Before_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        [TearDown]&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; TearDownContext()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            After_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;        }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre&gt;        { }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; After_each_test()&lt;/pre&gt;
&lt;pre class="alt"&gt;        { }&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;When the test fixture is started, the base class configures NHibernate and creates a session factory (&lt;b&gt;TestFixtureSetUp&lt;/b&gt;). When the whole test fixture is ended the session factory is closed (&lt;b&gt;TestFixtureTearDown&lt;/b&gt;).&lt;/p&gt;
&lt;p&gt;Before each test in the fixture is run the database schema is (re-) created and the virtual &lt;b&gt;Before_each_test&lt;/b&gt; method is called. After each test in the fixture is finished the virtual &lt;b&gt;After_each_test&lt;/b&gt; method is called. The two virtual methods can (but must not necessarily) be overridden in a child class.
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;All our test fixtures we implement will derive from this base class.&lt;/p&gt;
&lt;h3&gt;Filling the database with test data&lt;/h3&gt;
&lt;p&gt;To be able to test the loading behavior of NHibernate we need some test data in our database. We create this test data every time a test is run (just after the database schema is re-created). We add a new class &lt;b&gt;Order_Fixture&lt;/b&gt; to our test project and inherit from the &lt;b&gt;TestFixtureBase&lt;/b&gt; base class. Then we override the Before_each_test method and call a helper method which creates our initial data. We create just the absolute minimum of data we need (again --&amp;gt;DRY). That is: one customer placing one order with two order lines.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; LazyLoadEagerLoad.Domain;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.Criterion;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NHibernate.SqlCommand;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Order=LazyLoadEagerLoad.Domain.Order;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LazyLoadEagerLoad.Tests&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Order_Fixture : TestFixtureBase&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; Order _order;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Before_each_test()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="kwrd"&gt;base&lt;/span&gt;.Before_each_test();&lt;/pre&gt;
&lt;pre class="alt"&gt;            CreateInitialData();&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="rem"&gt;// create a single customer and an order with two order lines for this customer&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;            var customer = &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer {CompanyName = &lt;span class="str"&gt;&amp;quot;IBM&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;            var line1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderLine {Amount = 5, ProductName = &lt;span class="str"&gt;&amp;quot;Laptop XYZ&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre class="alt"&gt;            var line2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderLine {Amount = 2, ProductName = &lt;span class="str"&gt;&amp;quot;Desktop PC A100&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre&gt;            _order = &lt;span class="kwrd"&gt;new&lt;/span&gt; Order&lt;/pre&gt;
&lt;pre class="alt"&gt;                        {&lt;/pre&gt;
&lt;pre&gt;                            OrderNumber = &lt;span class="str"&gt;&amp;quot;o-100-001&amp;quot;&lt;/span&gt;,&lt;/pre&gt;
&lt;pre class="alt"&gt;                            OrderDate = DateTime.Today,&lt;/pre&gt;
&lt;pre&gt;                            Customer = customer&lt;/pre&gt;
&lt;pre class="alt"&gt;                        };&lt;/pre&gt;
&lt;pre&gt;            _order.OrderLines.Add(line1);&lt;/pre&gt;
&lt;pre class="alt"&gt;            _order.OrderLines.Add(line2);&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;                &lt;span class="kwrd"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre class="alt"&gt;                {&lt;/pre&gt;
&lt;pre&gt;                    session.Save(customer);&lt;/pre&gt;
&lt;pre class="alt"&gt;                    session.Save(_order);&lt;/pre&gt;
&lt;pre&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre class="alt"&gt;                }&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;The &lt;b&gt;CreateInitialData&lt;/b&gt; method is run before each test. With this we guarantee that each test is side effects free.&lt;/p&gt;
&lt;h3&gt;Verifying the default behavior - Lazy Loading&lt;/h3&gt;
&lt;p&gt;When loading an order entity from database the default behavior of NHibernate is to lazy load all associated objects of the order entity. Let&amp;#39;s write a test to verify this. For the verification we use a utility class provided by NHibernate (NHibernateUtil) which can test whether an associated object or object collection is initialized (i.e. loaded) or not. The class can also force the initialization of an un-initialized relation.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Customer_and_OrderLines_are_not_loaded_when_loading_Order()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;The test succeeds and NHibernate generates SQL similar to this one&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt;    order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0_0_, &lt;/pre&gt;
&lt;pre&gt;    order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_0_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;    order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate0_0_, &lt;/pre&gt;
&lt;pre&gt;    order0_.CustomerId &lt;span class="kwrd"&gt;as&lt;/span&gt; CustomerId0_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;FROM&lt;/span&gt;    Orders order0_ &lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;WHERE&lt;/span&gt;    order0_.Id=&lt;span class="str"&gt;&amp;#39;15bca5b3-2771-4bee-9923-85bda66318d8&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we have a problem: If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;[ExpectedException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(LazyInitializationException))]&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Accessing_customer_of_order_after_session_is_closed_throws()&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre class="alt"&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;    &lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="rem"&gt;// trying to access the Customer of the order, will throw exception&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="rem"&gt;// Note: at this point the session is already closed&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;string&lt;/span&gt; name = fromDb.Customer.CompanyName;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note: the above test &lt;b&gt;only&lt;/b&gt; succeeds if the method throws the expected exception of type &lt;b&gt;LazyInitializationException&lt;/b&gt;. And this is just what we want to show!
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;Another problem is the &lt;b&gt;n+1 select statements problem&lt;/b&gt;. If we access the order line items after loading the order we generate a select statement for each line item we access. Thus if we have n line items and want to access them all we generate one select statement for the order itself and n select statements for all line items (result: n+1 select statements). This can make our data fetching rather slow and put a (unnecessary) burden onto our database.&lt;/p&gt;
&lt;p&gt;We can simulate this behavior with this test method&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Show_n_plus_1_select_behavior()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre class="alt"&gt;    {&lt;/pre&gt;
&lt;pre&gt;        var fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;int&lt;/span&gt; sum = 0;&lt;/pre&gt;
&lt;pre&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var line &lt;span class="kwrd"&gt;in&lt;/span&gt; fromDb.OrderLines)&lt;/pre&gt;
&lt;pre class="alt"&gt;        {&lt;/pre&gt;
&lt;pre&gt;            &lt;span class="rem"&gt;// just some dummy code to force loading of order line&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;            sum += line.Amount;&lt;/pre&gt;
&lt;pre&gt;        } &lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate will generate SQL similar to the following
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;SELECT order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id3_0_, &lt;/pre&gt;
&lt;pre&gt;       order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_3_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate3_0_, &lt;/pre&gt;
&lt;pre&gt;       order0_.Customer &lt;span class="kwrd"&gt;as&lt;/span&gt; Customer3_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;FROM   Orders order0_ &lt;/pre&gt;
&lt;pre&gt;WHERE  order0_.Id=&lt;span class="str"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="alt"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;SELECT orderlines0_.OrderId &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderId1_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       orderlines0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id1_, &lt;/pre&gt;
&lt;pre&gt;       orderlines0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id4_0_, &lt;/pre&gt;
&lt;pre class="alt"&gt;       orderlines0_.Amount &lt;span class="kwrd"&gt;as&lt;/span&gt; Amount4_0_, &lt;/pre&gt;
&lt;pre&gt;       orderlines0_.ProductName &lt;span class="kwrd"&gt;as&lt;/span&gt; ProductN3_4_0_ &lt;/pre&gt;
&lt;pre class="alt"&gt;FROM   OrderLine orderlines0_ &lt;/pre&gt;
&lt;pre&gt;WHERE  orderlines0_.OrderId=&lt;span class="str"&gt;&amp;#39;5b2dbcb7-d0bf-4c53-86aa-8cd40cb5061a&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;This time we have been lucky! NHibernate has automatically generated an optimized query for us and has loaded the 2 order line items in one go. But this is not always the case! Imagine having a collection with several 100 items and you only need to access one or two of them. It would be a waste of resources to always load all items.&lt;/p&gt;
&lt;p&gt;But fortunately we have a solution for these kind of problems with NHibernate. It&amp;#39;s called eagerly loading.&lt;/p&gt;
&lt;h3&gt;Eagerly loading with the NHibernateUtil class&lt;/h3&gt;
&lt;p&gt;If you know you need have access to related objects of the order entity you can use the &lt;b&gt;NHibernateUtil&lt;/b&gt; class to initialize the related objects (that is: to fetch them from the database). Have a look at this test methods&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_initialize_customer_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;        NHibernateUtil.Initialize(fromDb.Customer);&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_initialize_order_lines_of_order_with_nhibernate_util()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.Get&amp;lt;Order&amp;gt;(_order.Id);&lt;/pre&gt;
&lt;pre&gt;        NHibernateUtil.Initialize(fromDb.OrderLines);&lt;/pre&gt;
&lt;pre class="alt"&gt;    }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;With this utility class you can initialize single objects as well as collection of objects. In each case NHibernate will send 2 select statements to the database. One to select the order and one to initialize the related object(s).&lt;/p&gt;
&lt;h3&gt;Eagerly loading with HQL&lt;/h3&gt;
&lt;p&gt;If you know that you want to load all order items of a given order then you can tell NHibernate to do so and eagerly load all order lines together with the order in one go. The following test method shows how you can formulate a HQL query which not only loads the order but also the associated customer and order lines.&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;[Test]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Can_eagerly_load_order_aggregate_with_hql_query()&lt;/pre&gt;
&lt;pre class="alt"&gt;{&lt;/pre&gt;
&lt;pre&gt;    Order fromDb;&lt;/pre&gt;
&lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = SessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;string&lt;/span&gt; sql = &lt;span class="str"&gt;&amp;quot;from Order o&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre&gt;                     &lt;span class="str"&gt;&amp;quot; inner join fetch o.OrderLines&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre class="alt"&gt;                     &lt;span class="str"&gt;&amp;quot; inner join fetch o.Customer&amp;quot;&lt;/span&gt; +&lt;/pre&gt;
&lt;pre&gt;                     &lt;span class="str"&gt;&amp;quot; where o.Id=:id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre class="alt"&gt;        fromDb = session.CreateQuery(sql)&lt;/pre&gt;
&lt;pre&gt;                        .SetGuid(&lt;span class="str"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, _order.Id)&lt;/pre&gt;
&lt;pre class="alt"&gt;                        .UniqueResult&amp;lt;Order&amp;gt;();&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre class="alt"&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));&lt;/pre&gt;
&lt;pre&gt;    Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));&lt;/pre&gt;
&lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;The resulting sql generated by NHibernate is then similar to this one&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt;      order0_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0_0_, &lt;br /&gt;            orderlines1_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id1_1_, &lt;br /&gt;            customer2_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id2_2_, &lt;br /&gt;            order0_.OrderNumber &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderNum2_0_0_, &lt;br /&gt;            order0_.OrderDate &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderDate0_0_, &lt;br /&gt;            order0_.CustomerId &lt;span class="kwrd"&gt;as&lt;/span&gt; CustomerId0_0_, &lt;br /&gt;            orderlines1_.Amount &lt;span class="kwrd"&gt;as&lt;/span&gt; Amount1_1_, &lt;br /&gt;            orderlines1_.ProductName &lt;span class="kwrd"&gt;as&lt;/span&gt; ProductN3_1_1_, &lt;br /&gt;            customer2_.CompanyName &lt;span class="kwrd"&gt;as&lt;/span&gt; CompanyN2_2_2_, &lt;br /&gt;            orderlines1_.OrderId &lt;span class="kwrd"&gt;as&lt;/span&gt; OrderId0__, &lt;br /&gt;            orderlines1_.Id &lt;span class="kwrd"&gt;as&lt;/span&gt; Id0__ &lt;br /&gt;&lt;span class="kwrd"&gt;from&lt;/span&gt;        Orders order0_ &lt;br /&gt;&lt;span class="kwrd"&gt;inner&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt;  OrderLine orderlines1_ &lt;span class="kwrd"&gt;on&lt;/span&gt; order0_.Id=orderlines1_.OrderId &lt;br /&gt;&lt;span class="kwrd"&gt;inner&lt;/span&gt; &lt;span class="kwrd"&gt;join&lt;/span&gt;  Customer customer2_ &lt;span class="kwrd"&gt;on&lt;/span&gt; order0_.CustomerId=customer2_.Id &lt;br /&gt;&lt;span class="kwrd"&gt;where&lt;/span&gt;       order0_.Id=&lt;span class="str"&gt;&amp;#39;409ebd99-3206-459b-bfed-6df989284da9&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, &amp;quot;Courier New&amp;quot;, courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;NHibernate has created an SQL select statement which joins the 3 tables involved, namely &lt;b&gt;Orders&lt;/b&gt;, &lt;b&gt;Customer&lt;/b&gt; and &lt;b&gt;OrderLine&lt;/b&gt;. The returned (flat) set of records is then used by NHibernate to build up the object tree with the order entity as a root.&lt;/p&gt;
&lt;h2&gt;Aggregates in the Domain&lt;/h2&gt;
&lt;p&gt;DDD defines the concept of &lt;a href="http://domaindrivendesign.org/discussion/messageboardarchive/Aggregates.html"&gt;aggregates&lt;/a&gt;. A short definition of an aggregate is &amp;quot;&lt;i&gt;A cluster of associated objects that are treated as a unit for the purpose of data changes.&amp;quot;. &lt;/i&gt;An aggregate always has a &lt;i&gt;root&lt;/i&gt;.&lt;i&gt;&amp;nbsp;&lt;/i&gt;In this context we can define the following aggregate in our domain&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_8.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="image" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/image_5F00_thumb_5F00_3.png" width="575" border="0" height="445" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The order entity is the &lt;i&gt;root&lt;/i&gt; and the order lines belong to the aggregate (can be regarded as children of the &lt;i&gt;root&lt;/i&gt;). When creating a new order or changing an existing one we only want to modify either the order itself or its order lines. We certainly do not want to change the customer entity because this would be a completely different use case and does not belong to the &lt;i&gt;order management&lt;/i&gt; use case.&lt;/p&gt;
&lt;p&gt;So, when dealing with aggregates we often want to load the complete aggregate in one go! This is the perfect example for using &lt;b&gt;eager loading&lt;/b&gt; techniques.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;I have introduced the concept of lazy loading as provided by NHibernate. I have discussed the consequences and shown how to avoid negative side effects by using different techniques of so called eager loading.&lt;/p&gt;
&lt;p&gt;&lt;a href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_2.png"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="Blog Signature Gabriel" src="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/nhibernate/Blog-Signature-Gabriel_5F00_thumb.png" width="244" border="0" height="21" /&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/0.aspx</link><pubDate>Fri, 08 Jul 2011 13:53:16 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:31</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 08/07/2011 10:53:16 a.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;medium&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;trust&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.uk2reviews.com"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;www.uk2.net&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reviews&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could keep lazy loading enabled (the default) and [[howtonh:Pre-Generate Lazy Loading Proxies|Pre-Generate Lazy Loading Proxies]]&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using ActiveRecord in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;activerecord&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span class="postbody"&gt;Place this line after your call to ActiveRecordStarter.Initialize(...):
&lt;br /&gt;
&lt;span style="text-decoration:underline;"&gt;NHibernate.Cfg.Environment.UseReflectionOptimizer = false;&lt;/span&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;isWeb&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly
you could keep lazy loading enabled (the default) and &lt;a href="/wikis/howtonh/pre-generate-lazy-loading-proxies.aspx"&gt;Pre-Generate Lazy Loading Proxies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/14.aspx</link><pubDate>Fri, 08 Jul 2011 13:02:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:488</guid><dc:creator>Ans1210</dc:creator><description>Revision 14 posted to How to by Ans1210 on 08/07/2011 10:02:02 a.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: medium trust&lt;/div&gt;

&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.uk2reviews.com"&gt;&lt;span style="background: SpringGreen;"&gt;www.uk2.net&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reviews&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could keep lazy loading enabled (the default) and [[howtonh:Pre-Generate Lazy Loading Proxies|Pre-Generate Lazy Loading Proxies]]&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using ActiveRecord in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;activerecord&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span class="postbody"&gt;Place this line after your call to ActiveRecordStarter.Initialize(...):
&lt;br /&gt;
&lt;span style="text-decoration:underline;"&gt;NHibernate.Cfg.Environment.UseReflectionOptimizer = false;&lt;/span&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;isWeb&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly
you could keep lazy loading enabled (the default) and &lt;a href="/wikis/howtonh/pre-generate-lazy-loading-proxies.aspx"&gt;Pre-Generate Lazy Loading Proxies&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/13.aspx</link><pubDate>Sat, 02 Jul 2011 10:23:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:487</guid><dc:creator>John Davidson</dc:creator><description>Revision 13 posted to How to by John Davidson on 02/07/2011 07:23:17 a.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: medium trust&lt;/div&gt;

&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could keep lazy loading enabled (the default) and [[howtonh:Pre-Generate Lazy Loading Proxies|Pre-Generate Lazy Loading Proxies]]&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Custom&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Custom&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using ActiveRecord in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Currently&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;playing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;best&lt;/span&gt; &lt;a rel="follow" href="http://online-casino.eu.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;online&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;casino&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;activerecord&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span class="postbody"&gt;Place this line after your call to ActiveRecordStarter.Initialize(...):
&lt;br /&gt;
&lt;span style="text-decoration:underline;"&gt;NHibernate.Cfg.Environment.UseReflectionOptimizer = false;&lt;/span&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;isWeb&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly
you could keep lazy loading enabled (the default) and &lt;a href="/wikis/howtonh/pre-generate-lazy-loading-proxies.aspx"&gt;Pre-Generate Lazy Loading Proxies&lt;/a&gt;&lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Custom&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/custom-dissertation/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Custom&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Custom&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/12.aspx</link><pubDate>Sat, 02 Jul 2011 06:12:27 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:474</guid><dc:creator>mikejohn</dc:creator><description>Revision 12 posted to How to by mikejohn on 02/07/2011 03:12:27 a.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: medium trust&lt;/div&gt;

&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could keep lazy loading enabled (the default) and [[howtonh:Pre-Generate Lazy Loading Proxies|Pre-Generate Lazy Loading Proxies]] &lt;a href="http://www.termpaperdom.com/"&gt;Custom Term Paper&lt;/a&gt; AND &lt;a href="http://www.researchpaperdom.com/"&gt;Custom Research Paper&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using ActiveRecord in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;span style="background: SpringGreen;"&gt;Currently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;playing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;best&lt;/span&gt; &lt;a rel="follow" href="http://online-casino.eu.com/"&gt;&lt;span style="background: SpringGreen;"&gt;online&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;casino&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;activerecord&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span class="postbody"&gt;Place this line after your call to ActiveRecordStarter.Initialize(...):
&lt;br /&gt;
&lt;span style="text-decoration:underline;"&gt;NHibernate.Cfg.Environment.UseReflectionOptimizer = false;&lt;/span&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;isWeb&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly
you could keep lazy loading enabled (the default) and &lt;a href="/wikis/howtonh/pre-generate-lazy-loading-proxies.aspx"&gt;Pre-Generate Lazy Loading Proxies &lt;/a&gt;&lt;a href="http://www.thesisdom.com/"&gt;Custom Thesis&lt;/a&gt; AND &lt;a href="http://www.dissertationdom.com/custom-dissertation/"&gt;Custom
Dissertation&lt;/a&gt; AND &lt;a href="http://www.essaydom.com/"&gt;Custom
Essay&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/11.aspx</link><pubDate>Wed, 30 Mar 2011 11:23:45 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:472</guid><dc:creator>markweee</dc:creator><description>Revision 11 posted to How to by markweee on 30/03/2011 08:23:45 a.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: medium trust&lt;/div&gt;

&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could keep lazy loading enabled (the default) and [[howtonh:Pre-Generate Lazy Loading Proxies|Pre-Generate Lazy Loading Proxies]] &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Custom&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Custom&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using ActiveRecord in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;activerecord&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span class="postbody"&gt;Place this line after your call to ActiveRecordStarter.Initialize(...):
&lt;br /&gt;
&lt;span style="text-decoration:underline;"&gt;NHibernate.Cfg.Environment.UseReflectionOptimizer = false;&lt;/span&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;isWeb&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly
you could keep lazy loading enabled (the default) and &lt;a href="/wikis/howtonh/pre-generate-lazy-loading-proxies.aspx"&gt;Pre-Generate Lazy Loading Proxies &lt;/a&gt;&lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Custom&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/custom-dissertation/"&gt;&lt;span style="background: SpringGreen;"&gt;Custom&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Custom&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/10.aspx</link><pubDate>Fri, 26 Sep 2008 17:04:34 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:374</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 10 posted to How to by Bill Pierce on 26/09/2008 02:04:34 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;medium&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trust&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could keep lazy loading enabled (the default) and [[howtonh:Pre-Generate Lazy Loading Proxies|Pre-Generate Lazy Loading Proxies]]&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using ActiveRecord in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;activerecord&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span class="postbody"&gt;Place this line after your call to ActiveRecordStarter.Initialize(...):
&lt;br /&gt;
&lt;span style="text-decoration:underline;"&gt;NHibernate.Cfg.Environment.UseReflectionOptimizer = false;&lt;/span&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;isWeb&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly
you could keep lazy loading enabled (the default) and &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;[howtonh:Pre-Generate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Loading&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Proxies|Pre-Generate&lt;/span&gt; &lt;a href="/wikis/howtonh/pre-generate-lazy-loading-proxies.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Pre-Generate&lt;/span&gt; Lazy Loading &lt;span style="text-decoration: line-through; color: red;"&gt;Proxies]&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Proxies&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/9.aspx</link><pubDate>Fri, 26 Sep 2008 17:01:44 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:156</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 9 posted to How to by Bill Pierce on 26/09/2008 02:01:44 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could keep lazy loading enabled (the default) and [[howtonh:Pre-Generate Lazy Loading Proxies|Pre-Generate Lazy Loading Proxies]]&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ActiveRecord&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Medium&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Trust&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;web&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;environment&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;requires&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;All&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;referenced&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assembiles&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;marked&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;AllowPartiallyTrustedCallers&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Web.config&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;marked&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;requirePermission=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;activerecord&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Castle.ActiveRecord&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;..&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Reflection&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;optimization&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;disabled&lt;/span&gt;&lt;br /&gt;&lt;span class="postbody"&gt;&lt;span style="background: SpringGreen;"&gt;Place&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;line&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;after&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ActiveRecordStarter.Initialize(...)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;
&lt;br /&gt;
&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Cfg.Environment.UseReflectionOptimizer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Default&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;disabled&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mappings&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;isWeb&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;..&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;config&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;Alternativly&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;could&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;keep&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;enabled&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;default&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;[howtonh:Pre-Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Proxies|Pre-Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Loading&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Proxies]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/8.aspx</link><pubDate>Mon, 22 Sep 2008 17:20:40 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:155</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 8 posted to How to by Bill Pierce on 22/09/2008 02:20:40 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could &lt;span style="text-decoration: line-through; color: red;"&gt;pre-generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;keep&lt;/span&gt; lazy loading &lt;span style="text-decoration: line-through; color: red;"&gt;proxies&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;enabled&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;default&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; [&lt;span style="text-decoration: line-through; color: red;"&gt;[howtonh:#|NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProxyGeneators]]&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;[howtonh:Pre-Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Proxies|Pre-Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Proxies]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/7.aspx</link><pubDate>Mon, 22 Sep 2008 15:58:51 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:144</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 7 posted to How to by Bill Pierce on 22/09/2008 12:58:51 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms998341.aspx"&gt;Medium Trust&lt;/a&gt; web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could pre-generate lazy loading proxies using [[howtonh:#|NHibernate ProxyGeneators]].&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/6.aspx</link><pubDate>Mon, 22 Sep 2008 15:57:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:138</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 6 posted to How to by Bill Pierce on 22/09/2008 12:57:49 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a Medium Trust web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could pre-generate lazy loading proxies using [[howtonh:#|NHibernate ProxyGeneators]].&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/5.aspx</link><pubDate>Mon, 22 Sep 2008 15:57:16 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:137</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 5 posted to How to by Bill Pierce on 22/09/2008 12:57:16 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a Medium Trust web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain.Models&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;false&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could pre-generate lazy loading proxies using [[howtonh:#|NHibernate ProxyGeneators]].&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/4.aspx</link><pubDate>Mon, 22 Sep 2008 15:55:43 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:136</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 4 posted to How to by Bill Pierce on 22/09/2008 12:55:43 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a Medium Trust web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;hibernate-configuration&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;hibernate-configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xmlns=&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;reflection-optimizer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use=&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;reflection-optimizer&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/hibernate-configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;hibernate-mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xmlns=&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;assembly=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;Example.Domain&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;Example.Domain.Models&amp;quot;&lt;span style="text-decoration:underline;"&gt;&lt;em&gt;&lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;default-lazy=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;default-lazy&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/class&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Alternativly you could pre-generate lazy loading proxies using [[howtonh:#|NHibernate ProxyGeneators]].&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/3.aspx</link><pubDate>Mon, 22 Sep 2008 15:53:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:135</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 3 posted to How to by Bill Pierce on 22/09/2008 12:53:54 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a Medium Trust web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;configSections&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;section&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;hibernate-configuration&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;requirePermission=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;false&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;section&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;requirePermission=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;requirePermission&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;false&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;...&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/configSections&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&amp;lt;hibernate-configuration xmlns=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;reflection-optimizer use=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;lt;/hibernate-configuration&amp;gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; assembly=&amp;quot;Example.Domain&amp;quot; namespace=&amp;quot;Example.Domain.Models&amp;quot; &lt;span style="text-decoration:underline;"&gt;&lt;em&gt;&lt;strong&gt;default-lazy=&amp;quot;false&amp;quot;&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;Alternativly you could pre-generate lazy loading proxies using [[howtonh:#|NHibernate ProxyGeneators]].&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/2.aspx</link><pubDate>Mon, 22 Sep 2008 15:53:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:134</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 2 posted to How to by Bill Pierce on 22/09/2008 12:53:12 p.m.&lt;br /&gt;
&lt;h2&gt;Run in Medium Trust&lt;/h2&gt;
&lt;p&gt;Using NHibernate in a Medium Trust web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;
&lt;p&gt;&amp;lt;configSections&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;section name=&amp;quot;hibernate-configuration&amp;quot; requirePermission=&amp;quot;false&amp;quot; type=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;section name=&amp;quot;log4net&amp;quot; requirePermission=&amp;quot;false&amp;quot; type=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp; ...&lt;br /&gt;&amp;lt;/configSections&amp;gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&amp;lt;hibernate-configuration xmlns=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;reflection-optimizer use=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;lt;/hibernate-configuration&amp;gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; assembly=&amp;quot;Example.Domain&amp;quot; namespace=&amp;quot;Example.Domain.Models&amp;quot; &lt;span style="text-decoration:underline;"&gt;&lt;em&gt;&lt;strong&gt;default-lazy=&amp;quot;false&amp;quot;&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;Alternativly you could pre-generate lazy loading proxies using [[howtonh:#|NHibernate ProxyGeneators]].&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Run in Medium Trust</title><link>http://nhforge.org/wikis/howtonh/run-in-medium-trust/revision/1.aspx</link><pubDate>Mon, 22 Sep 2008 15:49:26 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:133</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 1 posted to How to by Bill Pierce on 22/09/2008 12:49:26 p.m.&lt;br /&gt;
&lt;p&gt;Using NHibernate in a Medium Trust web environment requires the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All referenced assembiles must be marked with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.security.allowpartiallytrustedcallersattribute.aspx"&gt;AllowPartiallyTrustedCallers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Web.config configSections&amp;nbsp;must be marked with requirePermission=&amp;quot;false&amp;quot;&lt;br /&gt;&amp;lt;configSections&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;section name=&amp;quot;hibernate-configuration&amp;quot; &lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;requirePermission=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt; type=&amp;quot;NHibernate.Cfg.ConfigurationSectionHandler, NHibernate&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;section name=&amp;quot;log4net&amp;quot; &lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;requirePermission=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt; type=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;nbsp;&amp;lt;/configSections&amp;gt;&lt;/li&gt;
&lt;li&gt;Reflection optimization must be disabled&lt;br /&gt;&amp;lt;hibernate-configuration xmlns=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;reflection-optimizer use=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;lt;/hibernate-configuration&amp;gt;&lt;/li&gt;
&lt;li&gt;Default lazy loading must be disabled on all class mappings&lt;br /&gt;&amp;lt;hibernate-mapping xmlns=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot; assembly=&amp;quot;Example.Domain&amp;quot; namespace=&amp;quot;Example.Domain.Models&amp;quot; &lt;span style="text-decoration:underline;"&gt;&lt;em&gt;&lt;strong&gt;default-lazy=&amp;quot;false&amp;quot;&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;class name=&amp;quot;Person&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br /&gt;&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;Alternativly you could pre-generate lazy loading proxies using [[howtonh:#|NHibernate ProxyGeneators]].&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Finding Dirty Properties in NHibernate</title><link>http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate/revision/0.aspx</link><pubDate>Mon, 20 Jun 2011 11:35:01 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:66</guid><dc:creator>Ricardo Peres</dc:creator><description>Current revision posted to How to by Ricardo Peres on 20/06/2011 08:35:01 a.m.&lt;br /&gt;
&lt;h2&gt;Finding Dirty Properties in NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Session&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Architecture&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;UPDATE&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;deals&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;public static class SessionExtensions&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyEntity(this ISession session, Object entity)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;String&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;className&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateProxyHelper.GuessClass(entity).FullName&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;EntityEntry&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oldEntry&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;persistenceContext&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;GetEntry(entity);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;String&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;className&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oldEntry.EntityName&lt;/span&gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className)&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;EntityEntry&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;oldEntry&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sessionImpl.PersistenceContext.GetEntry(entity)&lt;/span&gt;;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;&lt;span&gt;     Int32&lt;/span&gt;&amp;nbsp;[]&amp;nbsp;dirtyProps&amp;nbsp;=&amp;nbsp;oldState.Select((o,&amp;nbsp;i)&amp;nbsp;=&amp;gt;&amp;nbsp;(oldState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;]&amp;nbsp;==&amp;nbsp;currentState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;])&amp;nbsp;?&amp;nbsp;-1&amp;nbsp;:&amp;nbsp;i).Where(x&amp;nbsp;=&amp;gt;&amp;nbsp;x&amp;nbsp;&amp;gt;=&amp;nbsp;0).ToArray();
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;			&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (dirtyProps != null);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;String&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;className&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateProxyHelper.GuessClass(entity).FullName&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;EntityEntry&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oldEntry&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;persistenceContext&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;GetEntry(entity);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;String&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;className&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oldEntry.EntityName&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;EntityEntry&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;oldEntry&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sessionImpl.PersistenceContext.GetEntry(entity)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (isDirty);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;String&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;className&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateProxyHelper.GuessClass(entity).FullName&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;EntityEntry&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oldEntry&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;persistenceContext&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;GetEntry(entity);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;String&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;className&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oldEntry.EntityName&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className)&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;EntityEntry&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;oldEntry&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sessionImpl.PersistenceContext.GetEntry(entity)&lt;/span&gt;;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Finding Dirty Properties in NHibernate</title><link>http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate/revision/6.aspx</link><pubDate>Thu, 14 Apr 2011 20:59:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:465</guid><dc:creator>Ricardo Peres</dc:creator><description>Revision 6 posted to How to by Ricardo Peres on 14/04/2011 05:59:56 p.m.&lt;br /&gt;
&lt;h2&gt;Finding Dirty Properties in NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;public static class SessionExtensions&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyEntity(this ISession session, Object entity)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;&lt;span&gt;     Int32&lt;/span&gt;&amp;nbsp;[]&amp;nbsp;dirtyProps&amp;nbsp;=&amp;nbsp;oldState.Select((o,&amp;nbsp;i)&amp;nbsp;=&amp;gt;&amp;nbsp;(oldState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;]&amp;nbsp;==&amp;nbsp;currentState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;])&amp;nbsp;?&amp;nbsp;-1&amp;nbsp;:&amp;nbsp;i).Where(x&amp;nbsp;=&amp;gt;&amp;nbsp;x&amp;nbsp;&amp;gt;=&amp;nbsp;0).ToArray();
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span&gt;			&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (dirtyProps != null);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (isDirty);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Finding Dirty Properties in NHibernate</title><link>http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate/revision/5.aspx</link><pubDate>Thu, 14 Apr 2011 20:59:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:441</guid><dc:creator>Ricardo Peres</dc:creator><description>Revision 5 posted to How to by Ricardo Peres on 14/04/2011 05:59:56 p.m.&lt;br /&gt;
&lt;h2&gt;Finding Dirty Properties in NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;public static class SessionExtensions&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyEntity(this ISession session, Object entity)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;&lt;span&gt;     Int32&lt;/span&gt;&amp;nbsp;[]&amp;nbsp;dirtyProps&amp;nbsp;=&amp;nbsp;oldState.Select((o,&amp;nbsp;i)&amp;nbsp;=&amp;gt;&amp;nbsp;(oldState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;]&amp;nbsp;==&amp;nbsp;currentState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;])&amp;nbsp;?&amp;nbsp;-1&amp;nbsp;:&amp;nbsp;i).Where(x&amp;nbsp;=&amp;gt;&amp;nbsp;x&amp;nbsp;&amp;gt;=&amp;nbsp;0).ToArray();
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span&gt;			&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (dirtyProps != null);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (isDirty);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Finding Dirty Properties in NHibernate</title><link>http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate/revision/4.aspx</link><pubDate>Thu, 14 Apr 2011 20:59:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:440</guid><dc:creator>Ricardo Peres</dc:creator><description>Revision 4 posted to How to by Ricardo Peres on 14/04/2011 05:59:56 p.m.&lt;br /&gt;
&lt;h2&gt;Finding Dirty Properties in NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;public static class SessionExtensions&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyEntity(this ISession session, Object entity)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;&lt;span&gt;     Int32&lt;/span&gt;&amp;nbsp;[]&amp;nbsp;dirtyProps&amp;nbsp;=&amp;nbsp;oldState.Select((o,&amp;nbsp;i)&amp;nbsp;=&amp;gt;&amp;nbsp;(oldState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;]&amp;nbsp;==&amp;nbsp;currentState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;])&amp;nbsp;?&amp;nbsp;-1&amp;nbsp;:&amp;nbsp;i).Where(x&amp;nbsp;=&amp;gt;&amp;nbsp;x&amp;nbsp;&amp;gt;=&amp;nbsp;0).ToArray();
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span&gt;			&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (dirtyProps != null);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (isDirty);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Finding Dirty Properties in NHibernate</title><link>http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate/revision/3.aspx</link><pubDate>Thu, 14 Apr 2011 20:59:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:436</guid><dc:creator>Ricardo Peres</dc:creator><description>Revision 3 posted to How to by Ricardo Peres on 14/04/2011 05:59:56 p.m.&lt;br /&gt;
&lt;h2&gt;Finding Dirty Properties in NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;public static class SessionExtensions&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyEntity(this ISession session, Object entity)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;&lt;span&gt;     Int32&lt;/span&gt;&amp;nbsp;[]&amp;nbsp;dirtyProps&amp;nbsp;=&amp;nbsp;oldState.Select((o,&amp;nbsp;i)&amp;nbsp;=&amp;gt;&amp;nbsp;(oldState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;]&amp;nbsp;==&amp;nbsp;currentState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;])&amp;nbsp;?&amp;nbsp;-1&amp;nbsp;:&amp;nbsp;i).Where(x&amp;nbsp;=&amp;gt;&amp;nbsp;x&amp;nbsp;&amp;gt;=&amp;nbsp;0).ToArray();
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span&gt;			&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (dirtyProps != null);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (isDirty);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Finding Dirty Properties in NHibernate</title><link>http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate/revision/2.aspx</link><pubDate>Thu, 14 Apr 2011 20:59:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:435</guid><dc:creator>Ricardo Peres</dc:creator><description>Revision 2 posted to How to by Ricardo Peres on 14/04/2011 05:59:56 p.m.&lt;br /&gt;
&lt;h2&gt;Finding Dirty Properties in NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;public static class SessionExtensions&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyEntity(this ISession session, Object entity)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;&lt;span&gt;     Int32&lt;/span&gt;&amp;nbsp;[]&amp;nbsp;dirtyProps&amp;nbsp;=&lt;span style="text-decoration: line-through; color: red;"&gt;persister.FindDirty(currentState&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;oldState&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;oldState.Select((o&lt;/span&gt;,&lt;span style="text-decoration: line-through; color: red;"&gt;sessionImpl)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;i&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&amp;nbsp;(oldState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;]&amp;nbsp;==&amp;nbsp;currentState&amp;nbsp;[&amp;nbsp;i&amp;nbsp;])&amp;nbsp;?&amp;nbsp;-1&amp;nbsp;:&amp;nbsp;i).Where(x&amp;nbsp;=&amp;gt;&amp;nbsp;x&amp;nbsp;&amp;gt;=&amp;nbsp;0).ToArray();&lt;/span&gt;
&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span&gt;			&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (dirtyProps != null);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (isDirty);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Finding Dirty Properties in NHibernate</title><link>http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate/revision/1.aspx</link><pubDate>Fri, 09 Oct 2009 11:48:50 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:416</guid><dc:creator>Ricardo Peres</dc:creator><description>Revision 1 posted to How to by Ricardo Peres on 09/10/2009 08:48:50 a.m.&lt;br /&gt;
&lt;p&gt;
&lt;p&gt;The following extension methods to ISession allow finding if a property or entity has changed, and the original value of a property.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;public static class SessionExtensions&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyEntity(this ISession session, Object entity)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;			&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (dirtyProps != null);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Boolean IsDirtyProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (isDirty);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Object GetOriginalEntityProperty(this ISession session, Object entity, String propertyName)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String className = NHibernateProxyHelper.GuessClass(entity).FullName;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;ISessionImplementor sessionImpl = session.GetSessionImplementation();&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IPersistenceContext persistenceContext = sessionImpl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;IEntityPersister persister = sessionImpl.Factory.GetEntityPersister(className);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;EntityEntry oldEntry = sessionImpl.PersistenceContext.GetEntry(entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if ((oldEntry == null) &amp;amp;&amp;amp; (entity is INHibernateProxy))&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;INHibernateProxy proxy = entity as INHibernateProxy;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object obj = sessionImpl.PersistenceContext.Unproxy(proxy);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;oldEntry = sessionImpl.PersistenceContext.GetEntry(obj);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] oldState = oldEntry.LoadedState;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Object [] currentState = persister.GetPropertyValues(entity, sessionImpl.EntityMode);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 [] dirtyProps = persister.FindDirty(currentState, oldState, entity, sessionImpl);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Int32 index = Array.IndexOf(persister.PropertyNames, propertyName);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Boolean isDirty = (dirtyProps != null) ? (Array.IndexOf(dirtyProps, index) != -1) : false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return ((isDirty == true) ? oldState [ index ] : currentState [ index ]);&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&lt;span&gt;	&lt;/span&gt;}&lt;/p&gt;
&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/0.aspx</link><pubDate>Sun, 17 Apr 2011 00:31:09 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:30</guid><dc:creator>mStyle</dc:creator><description>Current revision posted to How to by mStyle on 16/04/2011 09:31:09 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called SharedLibs&amp;nbsp; &lt;code&gt;c:\Code\SharedLibs\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with. This is your SharedLib folder from which you need to add your references to the NHibernate and NUnit dlls. Add references to NHibernate to both the demo project and the unit test project.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project. Note this code is dependent on Visual Studio 2008 and .Net Framework 3.5.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain). You will have added the design folder and created the class diagram yourself although this is for good practice and not required for the purposes of this excercise.&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false! Add a copy of hibernate.cfg.xml to the root of this unit test project. Direct action with NHibernate in the NUnit project needs access to this file.&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us. SchemaExport will create the product table in the database and each time you call it it will delete the table and the table data and recreate it.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly. However, you can check the database and see the newly created &amp;#39;product&amp;#39; table.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cycle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;a href="http://www.mustuniversity.com/programs/degree/masters-degree.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Master&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/associate-degree.html"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Associate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-course-certificate.html"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Graduate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Course&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Certificate&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To be added: implemetation of GetByName using HQL. Implement HCQ as below this works as expected and returns a product entity.&lt;/p&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product. You need to add a reference to&amp;nbsp; NHibernate.Criterion on your repository page.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;a href="http://www.mustuniversity.com/programs/certificate/undergraduate-course-certificate.html"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Undergraduate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Course&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Certificate&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/certificate/undergraduate-certificate.html"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Undergraduate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Certificate&lt;/span&gt;&amp;nbsp;&lt;a href="http://www.mustuniversity.com/programs/certificate/undergraduate-course-certificate.html"&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/16.aspx</link><pubDate>Wed, 30 Mar 2011 11:01:40 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:452</guid><dc:creator>markweee</dc:creator><description>Revision 16 posted to How to by markweee on 30/03/2011 08:01:40 a.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called SharedLibs&amp;nbsp; &lt;code&gt;c:\Code\SharedLibs\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with. This is your SharedLib folder from which you need to add your references to the NHibernate and NUnit dlls. Add references to NHibernate to both the demo project and the unit test project.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project. Note this code is dependent on Visual Studio 2008 and .Net Framework 3.5.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain). You will have added the design folder and created the class diagram yourself although this is for good practice and not required for the purposes of this excercise.&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false! Add a copy of hibernate.cfg.xml to the root of this unit test project. Direct action with NHibernate in the NUnit project needs access to this file.&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us. SchemaExport will create the product table in the database and each time you call it it will delete the table and the table data and recreate it.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly. However, you can check the database and see the newly created &amp;#39;product&amp;#39; table.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cycle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows &lt;a href="http://www.mustuniversity.com/programs/degree/masters-degree.html"&gt;&lt;span style="background: SpringGreen;"&gt;Master&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/degree/associate-degree.html"&gt;
&lt;span style="background: SpringGreen;"&gt;Associate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Degree&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/certificate/graduate-course-certificate.html"&gt;
&lt;span style="background: SpringGreen;"&gt;Graduate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Course&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Certificate&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To be added: implemetation of GetByName using HQL. Implement HCQ as below this works as expected and returns a product entity.&lt;/p&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product. You need to add a reference to&amp;nbsp; NHibernate.Criterion on your repository page.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here). &lt;a href="http://www.mustuniversity.com/programs/certificate/undergraduate-course-certificate.html"&gt;
&lt;span style="background: SpringGreen;"&gt;Undergraduate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Course&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Certificate&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.mustuniversity.com/programs/certificate/undergraduate-certificate.html"&gt;
&lt;span style="background: SpringGreen;"&gt;Undergraduate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Certificate&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/15.aspx</link><pubDate>Tue, 08 Mar 2011 10:17:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:367</guid><dc:creator>mmr_masum</dc:creator><description>Revision 15 posted to How to by mmr_masum on 08/03/2011 07:17:12 a.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;strong&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;strong&gt;Schenker&lt;/strong&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;&lt;strong&gt;Installing NHibernate&lt;/strong&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called SharedLibs&amp;nbsp; &lt;code&gt;c:\Code\SharedLibs\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with. This is your SharedLib folder from which you need to add your references to the NHibernate and NUnit dlls. Add references to NHibernate to both the demo project and the unit test project.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project. Note this code is dependent on Visual Studio 2008 and .Net Framework 3.5.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;&lt;strong&gt;Create Your Project&lt;/strong&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Defining the Business Objects&lt;br /&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;strong&gt;Product&lt;/strong&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;strong&gt;Product.cs&lt;/strong&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;strong&gt;NHibernate&lt;/strong&gt;for this task. An instance of an entity in the domain corresponds to a &lt;strong&gt;row in a table&lt;/strong&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Define the Mapping&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;strong&gt;Mappings&lt;/strong&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;strong&gt;&lt;strong&gt;Product&lt;/strong&gt;.hbm.xml&lt;/strong&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;strong&gt;Build Action&lt;/strong&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;strong&gt;assembly&lt;/strong&gt; and &lt;strong&gt;namespace&lt;/strong&gt; of the root node. It&amp;#39;s similar to the &lt;strong&gt;using&lt;/strong&gt; statement in C#.&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;strong&gt;primary key&lt;/strong&gt; for the product entity. Technically we could take the property &lt;strong&gt;Name&lt;/strong&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;strong&gt;Id&lt;/strong&gt;. We use &lt;strong&gt;Guid&lt;/strong&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;strong&gt;Domain.cd&lt;/strong&gt; contains the class diagram of our simple domain). You will have added the design folder and created the class diagram yourself although this is for good practice and not required for the purposes of this excercise.&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Configure NHibernate&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;strong&gt;Copy to Output&lt;/strong&gt;&amp;quot; to &amp;quot;&lt;strong&gt;Copy always&lt;/strong&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;strong&gt;SQL&lt;/strong&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;strong&gt;FirstSample.sdf&lt;/strong&gt; to the FirstSample project (choose &lt;strong&gt;Local Database&lt;/strong&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Test the Setup&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;strong&gt;Microsoft SQL Server Compact Edition&lt;/strong&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;strong&gt;System.Data.SqlServerCe.dll&lt;/strong&gt; is located in the sub-folder &lt;strong&gt;Desktop&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;strong&gt;Copy Local&lt;/strong&gt;&amp;quot; to &lt;strong&gt;true&lt;/strong&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false! Add a copy of hibernate.cfg.xml to the root of this unit test project. Direct action with NHibernate in the NUnit project needs access to this file.&lt;/p&gt;
&lt;p&gt;Add a class called &lt;strong&gt;GenerateSchema_Fixture&lt;/strong&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;strong&gt;hibernate.cfg.xml&lt;/strong&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us. SchemaExport will create the product table in the database and each time you call it it will delete the table and the table data and recreate it.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;strong&gt;NOT&lt;/strong&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly. However, you can check the database and see the newly created &amp;#39;product&amp;#39; table.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;strong&gt;Run Test(s)&lt;/strong&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;strong&gt;Run&lt;/strong&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;In case of Problems&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;strong&gt;Build Action&lt;/strong&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;strong&gt;Embedded Resource&lt;/strong&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Our first CRUD operations&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cycle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;strong&gt;FirstSolution&lt;/strong&gt; project. Add a class &lt;strong&gt;ProductRepository&lt;/strong&gt; to this folder. Make the&lt;strong&gt;ProductRepository&lt;/strong&gt; inherit from the interface &lt;strong&gt;IProductRepository&lt;/strong&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;strong&gt;Manipulating Data&lt;/strong&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;strong&gt;Add&lt;/strong&gt; method in the repository class. Let&amp;#39;s do it. &lt;strong&gt;But wait&lt;/strong&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;strong&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/strong&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;strong&gt;Note&lt;/strong&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;strong&gt;Querying the Database&lt;/strong&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To be added: implemetation of GetByName using HQL. Implement HCQ as below this works as expected and returns a product entity.&lt;/p&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product. You need to add a reference to&amp;nbsp; NHibernate.Criterion on your repository page.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/14.aspx</link><pubDate>Wed, 16 Feb 2011 08:33:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:364</guid><dc:creator>shanti</dc:creator><description>Revision 14 posted to How to by shanti on 16/02/2011 05:33:12 a.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called SharedLibs&amp;nbsp; &lt;code&gt;c:\Code\SharedLibs\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with. This is your SharedLib folder from which you need to add your references to the NHibernate and NUnit dlls. Add references to NHibernate to both the demo project and the unit test project.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project. Note this code is dependent on Visual Studio 2008 and .Net Framework 3.5.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain). You will have added the design folder and created the class diagram yourself although this is for good practice and not required for the purposes of this excercise.&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false! Add a copy of hibernate.cfg.xml to the root of this unit test project. Direct action with NHibernate in the NUnit project needs access to this file.&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us. SchemaExport will create the product table in the database and each time you call it it will delete the table and the table data and recreate it.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly. However, you can check the database and see the newly created &amp;#39;product&amp;#39; table.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cycle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To be added: implemetation of GetByName using HQL. Implement HCQ as below this works as expected and returns a product entity.&lt;/p&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product. You need to add a reference to&amp;nbsp; NHibernate.Criterion on your repository page.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/13.aspx</link><pubDate>Mon, 21 Dec 2009 22:30:32 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:359</guid><dc:creator>Joe Doyle</dc:creator><description>Revision 13 posted to How to by Joe Doyle on 21/12/2009 07:30:32 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;&lt;span style="background: SpringGreen;"&gt;Wiki&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;extracted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;original&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Gabriel&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Schenker&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;span style="background: SpringGreen;"&gt;Welcome&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assume&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ve&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;a href="/media/p/4.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;started&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tutorial&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;talk&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;through&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Installing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Defining&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;load&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;save&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Configure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;talk&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;local&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Automatically&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CRUD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;pattern&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Unit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Tests&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correctly&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;aiming&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;But&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;things&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:)]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Lets&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;actually&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;doing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;something&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ZIP&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Installing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ve&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;binaries&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;zip&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;extract&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;somewhere&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sensible&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;usually&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SharedLibs&lt;/span&gt;&amp;nbsp; &lt;code&gt;&lt;span style="background: SpringGreen;"&gt;c:\Code\SharedLibs\NHibernate&lt;/span&gt;&lt;/code&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;extract&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;zip&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;there&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;But&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whatever&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;comfortable&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SharedLib&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;references&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NUnit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dlls&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;references&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;both&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demo&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;That&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;installed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;easy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;huh)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;talk&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;through&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Visual&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Studio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;moment&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;First&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;go&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Note&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dependent&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Visual&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Studio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;2008&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Net&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Framework&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3.5&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;building&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;application&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;objects&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blank&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;put&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;them&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fire&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;up&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Visual&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Studio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Library&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;something&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;interesting&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Defining&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Objects&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Lets&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defining&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;very&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;moment&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;consists&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Discontinued&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Product.cs&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;very&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;uses&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;automatic&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;feature&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;C&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;#&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3.0&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;compiler&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Discontinued&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;able&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;persist&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;instances&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;relational)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;chosen&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;task&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;An&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;instance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;corresponds&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;row&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;So&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;between&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;corresponding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;done&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;either&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defining&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml-document&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;decorating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;attributes&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Mapping&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Mappings&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml-document&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;b&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;hbm.xml&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Please&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;note&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;hbm&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;part&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;convention&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;automatically&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recognize&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;Embedded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Resource&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Build&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Action&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Windows&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Explorer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;locate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;nhibernate-mapping.xsd&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;src&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;copy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SharedLibs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;definition&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defining&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;VS&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;then&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;intellisense&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;validation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;editing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;document&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Back&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;VS&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product.hbm.xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;lt;hibernate-mapping&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;node&lt;/span&gt;&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;FirstSolution&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;more&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;info&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt; -&lt;span style="text-decoration: line-through; color: red;"&gt;Removed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;spammer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;links&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;referencing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;always&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fully&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;qualified&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;e.g.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSample.Domain.Product&lt;/span&gt;, &lt;span style="background: SpringGreen;"&gt;FirstSample)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;To&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;less&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;verbose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;classes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implemented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;classes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;two&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;attributes&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;node&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;similar&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;statement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;C#&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;primary&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Technically&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;could&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;take&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defined&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unique&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;But&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;common&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;surrogate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;instead&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thus&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Id&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Guid&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Id&lt;/span&gt; but &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; can &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;well&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;int&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;long&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;System&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Guid&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Id&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Discontinued&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;complete&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;FirstSolution&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;id&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;generator&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;id&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Discontinued&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;doesn&lt;/span&gt;&amp;#39;t &lt;span style="text-decoration: line-through; color: red;"&gt;restore&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;way&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;such&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defines&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;many&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reasonable&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defaults&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;So&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;don&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;t&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;explicitly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; the &lt;span style="text-decoration: line-through; color: red;"&gt;original&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;content&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;due&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;according&lt;/span&gt; to &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;automatically&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;infer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;definition&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;As&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;consequence&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;my&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cluttered&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;redundant&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Please&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;refer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;online&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;documentation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;more&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;detailed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;explanation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;find&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;&lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;explorer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;like&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Domain.cd&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;contains&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;diagram&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;added&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;design&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;created&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;diagram&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;yourself&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;although&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;good&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;practice&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;required&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;purposes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;excercise&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Configure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tell&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;connection&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;details&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;form&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;connection&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;supports&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;many&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;many&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;products&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hibernate.cfg.xml&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Set&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;its&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Copy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Output&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Copy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;always&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Server&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Compact&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Edition&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;enter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;into&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Dialect.MsSqlCeDialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Driver.SqlServerCeDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Data&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Source=FirstSample.sdf&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;With&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tell&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;MS&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Server&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Compact&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Edition&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;target&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;shall&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSample.sdf&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;=connection&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;string)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defined&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;SQL&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;generates&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sends&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;highly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recommended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;debugging&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;purposes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;during&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;development)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Double&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;no&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;typos&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; an &lt;span style="text-decoration: line-through; color: red;"&gt;error&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;empty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;FirstSample.sdf&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;choose&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Local&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Database&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;template&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Click&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ignore&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;DataSet&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;wizard&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Cancel)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Setup&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;setup&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;First&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;verify&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SharedLibs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;last&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;8&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;find&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Microsoft&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Server&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Compact&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Edition&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;directory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Programs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Note&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;System.Data.SqlServerCe.dll&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;located&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sub-folder&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Desktop&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;All&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;other&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reference&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Additionally&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;references&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.dll&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;nunit.framework.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Systm.Data.SqlServerCe.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;remember&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reference&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;located&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SharedLibs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder!)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Pay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;attention&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Copy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Local&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;System.Data.SqlServerCe.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;default&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;copy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hibernate.cfg.xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Direct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;action&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NUnit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;needs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;access&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;GenerateSchema_Fixture&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;like&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;further&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;7&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sqce*.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;output&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;directory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post-build&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;VS&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Enter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;command&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;Post-build&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;command&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;line&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;copy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;$&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(ProjectDir)..\..\SharedLibs\sqlce*.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;$&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(ProjectDir)$(OutDir)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GenerateSchema_Fixture&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.Cfg&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.Tool.hbm2ddl&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NUnit.Framework&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Tests&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;TestFixture]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GenerateSchema_Fixture&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_generate_schema(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cfg&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Configuration()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;cfg.Configure()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;cfg.AddAssembly&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;typeof&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product).Assembly);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SchemaExport(cfg).Execute&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;line&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creates&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;instance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;second&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;line&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tell&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;itself&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;out&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;any&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;So&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;search&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;hibernate.cfg.xml&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;output&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;directory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;That&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;exactly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defined&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;settings&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;third&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;line&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tell&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;find&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contains&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;At&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;being&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;find&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;such&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product.hbm.xml)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;embedded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;resource&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fourth&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;line&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;uses&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SchemaExport&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;helper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auto&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;magically&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;us&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SchemaExport&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;delete&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;data&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recreate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Note&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;NOT&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;find&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;out&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whether&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;does&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;its&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;job&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correctly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;does&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;rater&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whether&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;setup&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;system&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correctly&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;However&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;newly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;created&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;a href="http://www.testdriven.net/"&gt;&lt;span style="background: SpringGreen;"&gt;TestDriven.Net&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;installed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;right&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;click&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inside&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;choose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Test(s&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;execute&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;every&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ok&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;result&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;output&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;window&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;a href="http://www.jetbrains.com/resharper/"&gt;&lt;span style="background: SpringGreen;"&gt;ReSharper&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;installed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;clicking&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;yellow-green&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;circle&lt;/span&gt; on the &lt;span style="text-decoration: line-through; color: red;"&gt;site&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;left&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;border&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;choose&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Run&lt;/span&gt;&lt;/b&gt;.&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;Use&lt;/span&gt; &lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;result&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;case&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Problems&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fails&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;double&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;find&lt;/span&gt; the &lt;span style="text-decoration: line-through; color: red;"&gt;History&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tab&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;target&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;directory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Double&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;no&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;typos&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;hibernate.cfg.xml)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product.hbm.xml).&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Finally&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whether&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Build&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Action&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product.hbm.xml)&lt;/span&gt; to &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Embedded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Resource&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;continue&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;succeeds&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CRUD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;operations&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;obviously&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;system&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ready&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;successfully&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implemented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Domain&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defined&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configured&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Finally&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;automatically&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;spirit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;DDD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;e.g&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;a href="http://domaindrivendesign.org/books/"&gt;&lt;span style="background: SpringGreen;"&gt;Domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Driven&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Design&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Eric&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Evans&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;crud&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;operations&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;create,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;read&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;delete)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;interface&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;part&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;where&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implementation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implementation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;infrastructure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;specific&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;keep&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;persistence&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ignorant&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;PI).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;interface&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;interface&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;System&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;System.Collections.Generic&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;interface&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Remove(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetById(Guid&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;productId)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByName&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;ICollection&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByCategory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;category)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository_Fixture&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;TestFixture]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository_Fixture&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ISessionFactory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;configuration;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;TestFixtureSetUp]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;TestFixtureSetUp(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Configuration()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;configuration.Configure();&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;configuration.AddAssembly(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;typeof&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product).Assembly);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;configuration.BuildSessionFactory();&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fourth&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;line&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;TestFixtureSetUp&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;factory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expensive&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;process&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thus&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;executed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;once&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;That&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reason&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;why&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;put&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;into&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;executed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;once&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;during&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cycle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;To&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;keep&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;methods&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;side&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;effect&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;free&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;re-create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;execution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Thus&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;SetUp]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SetupContext(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SchemaExport(_configuration).Execute&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;And&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;instance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;adding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Repositories&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;FirstSolution&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;ProductRepository&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;ProductRepository&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;inherit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;interface&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;System&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;System.Collections.Generic&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Repositories&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Remove(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetById(Guid&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;productId&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByName&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ICollection&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByCategory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;category&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Manipulating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Data&lt;/span&gt;&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;go&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;back&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository_Fixture&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_add_new_product(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;repository.Add(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fail&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;yet&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implemented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;But&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;wait&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;little&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;helper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provides&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;us&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;objects&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demand&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate.Cfg&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FirstSolution.Repositories&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernateHelper&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ISessionFactory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ISessionFactory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SessionFactory&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;_sessionFactory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Configuration()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="background: SpringGreen;"&gt;configuration.Configure()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    &lt;span style="background: SpringGreen;"&gt;configuration.AddAssembly&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;typeof&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product).Assembly);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configuration.BuildSessionFactory()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OpenSession(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SessionFactory.OpenSession()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creates&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;factory&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;client&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;needs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ITransaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;transaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;session.Save(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;second&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;again&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fail&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;message&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;That&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;because&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;default&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configured&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt;&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;&lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;load&lt;/span&gt;&lt;/a&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entities&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;That&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recommended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;approach&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;warmly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recommend&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;maximum&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;flexibility&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;How&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solve&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;easy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;methods&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object(s&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;virtual&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Guid&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Id&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Discontinued&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;again&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;succeed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;output&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Note&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sql&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;spit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;out&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;think&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;successfully&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inserted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;into&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;But&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whether&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;really&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;so&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;extend&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_add_new_product(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;repository.Add(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;try&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;load&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.Get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;(product.Id);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;successfully&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inserted&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="background: SpringGreen;"&gt;Assert.IsNotNull(fromDb)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="background: SpringGreen;"&gt;Assert.AreNotSame(product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="background: SpringGreen;"&gt;Assert.AreEqual(product.Name&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb.Name)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="background: SpringGreen;"&gt;Assert.AreEqual(product.Category&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb.Category)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;again&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Hopefully&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;succeed..&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ready&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;other&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;methods&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;testing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;would&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;rather&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;already&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;containing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;products&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Nothing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;easier&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;than&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CreateInitialData&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;readonly&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;products&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Coca&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Cola&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Pepsi&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Cola&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CreateInitialData(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ITransaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;transaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;foreach&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;products)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        &lt;span style="background: SpringGreen;"&gt;session.Save(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="background: SpringGreen;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SetupContext&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;after&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;done&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;after&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;created&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;populated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;products&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_update_existing_product(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;products[0];&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;product.Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Yellow&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Pear&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;repository.Update(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;try&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;load&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.Get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;(product.Id);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="background: SpringGreen;"&gt;Assert.AreEqual(product.Name&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb.Name)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;When&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;running&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fail&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;yet&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;been&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implemented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Note&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expected&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;behavior&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;since&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;TDD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;always&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fail&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Analogous&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;difference&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;instead&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;save&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ITransaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;transaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;session.Update(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;again&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;watch&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;succeed&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;delete&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;straight&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;forward&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;When&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;testing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whether&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;record&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;really&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;been&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;deleted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;returned&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;equal&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;null&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Here&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_remove_existing_product(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;products[0];&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;repository.Remove(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;_&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.Get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;(product.Id);&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;Assert.IsNull(fromDb)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implementation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Remove&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Remove(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ITransaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;transaction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;session.Delete(product)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Querying&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Database&lt;/span&gt;&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;still&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;three&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;methods&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;objects&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;most&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;easy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetById&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;First&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;write&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_get_existing_product_by_id(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository.GetById(_products[1].Id)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.IsNotNull(fromDb)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.AreNotSame(_products[1]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.AreEqual(_products[1].Name&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb.Name)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;then&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fulfill&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetById(Guid&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;productId&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.Get&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;(productId);&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;easy&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;two&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;methods&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByName&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;As&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;usual&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;write&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_get_existing_product_by_name(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository.GetByName(_products[1].Name)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.IsNotNull(fromDb)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.AreNotSame(_products[1]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.AreEqual(_products[1].Id&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb.Id)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implementation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByName&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;done&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;two&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;different&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;approaches&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Hibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Language&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;second&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HCQ&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Hibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Criteria&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Query)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oriented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;language&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;similar&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;equal&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SQL&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;To&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;added&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implemetation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByName&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HCQ&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;below&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;works&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expected&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;returns&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;above&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;introduced&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;commonly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;technique&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;&lt;span style="background: SpringGreen;"&gt;fluent&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;interfaces&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;As&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;result&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;less&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;verbose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;easier&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;understand&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;embedded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;named)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;parameters&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Parameters&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;prefixed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;defines&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;many&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;helper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;methods&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;like&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SetString&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;example&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assign&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;values&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;various&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;types&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;those&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;parameters&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Finally&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;UniqueResult&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tell&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expect&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;record&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;more&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;than&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;record&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;returned&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;then&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;exception&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;raised&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;To&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;more&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;please&lt;/span&gt; read &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;&lt;span style="background: SpringGreen;"&gt;online&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;documentation&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;second&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;uses&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;criteria&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;search&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;requested&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reference&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;NHibernate.Criterion&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;page&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByName&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;CreateCriteria(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;typeof&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product))&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Add(Restrictions.Eq(&lt;/span&gt;&lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;UniqueResult&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;();&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Many&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;users&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;think&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;approach&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;more&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;oriented&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;On&lt;/span&gt; the &lt;span style="background: SpringGreen;"&gt;other&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hand&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;complex&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;written&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;criteria&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;syntax&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;quickly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;become&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;difficult&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;understand&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;last&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByCategory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;returns&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;products&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implemented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Can_get_existing_products_by_category(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;IProductRepository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository.GetByCategory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.AreEqual(2&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb.Count)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.IsTrue(IsInCollection(_products[0]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb))&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;Assert.IsTrue(IsInCollection(_products[1]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb))&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IsInCollection(Product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ICollection&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;foreach&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;item&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fromDb&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;product.Id&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;item.Id&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;method&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;itself&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;might&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt;&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ICollection&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByCategory&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;category&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ISession&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;products&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;CreateCriteria(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;typeof&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Product))&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Add(Restrictions.Eq(&lt;/span&gt;&lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;category)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;br /&gt;                    &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;List&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Product&amp;gt;();&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;products&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Summary&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;In&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; article &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;shown&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;basic&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;able&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;persist&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;objects&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;shown&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;typically&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;write&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CRUD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;methods&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;domain&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;objects&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;taken&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;MS&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SQL&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Compact&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Edition&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;any&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;other&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;supported&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hibernate.cfg.xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;accordingly)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Ee&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;no&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dependencies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;external&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;frameworks&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tools&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;other&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;than&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;itself&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.NET&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;course&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;never&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;counts&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here)&lt;/span&gt;.&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/12.aspx</link><pubDate>Mon, 21 Dec 2009 22:30:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:301</guid><dc:creator>Joe Doyle</dc:creator><description>Revision 12 posted to How to by Joe Doyle on 21/12/2009 07:30:17 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;p&gt;&lt;a href="http://www.dicapriophotography.com"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;san&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;diego&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;wedding&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;photographer&lt;/span&gt;&lt;/a&gt;
&lt;a href="http://www.dicapriophotography.com"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;san&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;diego&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;wedding&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;photographers&lt;/span&gt;&lt;p&gt;&lt;a href="http://www.dicapriophotography.com"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Removed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;spammer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;links&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;t&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;restore&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;original&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;content&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;due&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;error&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;site&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;History&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tab&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;read&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;article&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;/a&gt;&lt;a href="http://www.dicapriophotography.com"&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/11.aspx</link><pubDate>Mon, 21 Dec 2009 07:32:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:300</guid><dc:creator>raymond456</dc:creator><description>Revision 11 posted to How to by raymond456 on 21/12/2009 04:32:56 a.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Wiki&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;extracted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;original&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;blog&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;post&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Gabriel&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Schenker&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Welcome&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;If&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;re&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reading&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assume&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ve&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;a href="/media/p/4.aspx"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;downloaded&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;started&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tutorial&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;talk&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;through&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Installing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Defining&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;simple&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;business&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;load&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;save&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;business&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Configure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;talk&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;local&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Automatically&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;generate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;simple&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;CRUD&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pattern&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Unit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Tests&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;make&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;working&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;correctly&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;what&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;re&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;aiming&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;But&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;things&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:)]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Lets&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;actually&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;doing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;something&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ZIP&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;downloaded&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Installing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;If&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ve&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;downloaded&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;binaries&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;zip&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;need&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;extract&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;somewhere&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sensible&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;usually&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;called&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SharedLibs&lt;/span&gt;&amp;nbsp; &lt;code&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;c:\Code\SharedLibs\NHibernate&lt;/span&gt;&lt;/code&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;extract&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;zip&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;there&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;But&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;whatever&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;re&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;comfortable&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SharedLib&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;need&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;references&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NUnit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;dlls&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;references&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;both&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demo&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;unit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;That&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;installed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;easy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;huh)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;talk&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;through&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Visual&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Studio&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;moment&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;First&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;look&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;how&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;go&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;about&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;creating&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Note&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;dependent&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Visual&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Studio&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;2008&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Net&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Framework&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;3.5&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Project&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Before&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;building&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;application&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;business&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;objects&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;need&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;blank&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;put&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;them&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Fire&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;up&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Visual&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Studio&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Library&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;look&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;something&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;interesting&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;creating&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;business&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Defining&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Business&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Objects&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Lets&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defining&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;very&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;simple&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;For&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;moment&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consists&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;has&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;3&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;properties&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Discontinued&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product.cs&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;very&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;simple&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;uses&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;automatic&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;properties&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;feature&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;C&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;#&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;3.0&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;compiler&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt;&lt;br /&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Discontinued&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;able&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;persist&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;instances&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;relational)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;chosen&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;task&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;An&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;instance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;corresponds&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;row&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;So&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;between&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;corresponding&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;done&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;either&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defining&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml-document&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;decorating&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;attributes&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Mappings&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml-document&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;call&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;b&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hbm.xml&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Please&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;note&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;hbm&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;part&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;convention&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;used&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;automatically&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;recognize&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Embedded&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Resource&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Build&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Action&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Windows&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Explorer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;locate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;nhibernate-mapping.xsd&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;src&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;copy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SharedLibs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;schema&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;definition&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;when&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defining&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;VS&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;then&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;provide&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;intellisense&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;validation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;when&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;editing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;document&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Back&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;VS&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;schema&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product.hbm.xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Each&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;has&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;hibernate-mapping&amp;gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;root&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;node&lt;/span&gt;&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;?&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;FirstSolution&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;more&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;info&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;when&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;referencing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;always&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;provide&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fully&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;qualified&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;e.g.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample.Domain.Product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;To&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;make&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;less&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;verbose&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assembly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;classes&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implemented&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;classes&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;two&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;attributes&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;assembly&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;root&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;node&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;It&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;similar&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;statement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;C#&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;primary&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;key&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Technically&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;could&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;take&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;must&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defined&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;has&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;unique&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;But&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;common&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;surrogate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;key&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;instead&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;For&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;thus&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;call&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Id&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Guid&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Id&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;well&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;int&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;long&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;System&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt;&lt;br /&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Guid&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Id&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Discontinued&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;complete&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;?&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;FirstSolution&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;id&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;generator&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;guid&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;id&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Discontinued&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate-mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;doesn&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;t&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;way&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;such&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defines&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;many&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reasonable&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defaults&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;So&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;don&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;t&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;provide&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;column&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;explicitly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;column&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;according&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;automatically&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;infer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;column&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;definition&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;As&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;consequence&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;my&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cluttered&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;redundant&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;information&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Please&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;refer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;online&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;more&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;detailed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;explanation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;You&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;explorer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;look&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;like&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Domain.cd&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contains&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;diagram&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;simple&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;You&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;added&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;design&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;created&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;diagram&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;yourself&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;although&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;good&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;practice&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;required&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;purposes&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;excercise&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Configure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tell&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;provide&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;connection&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;details&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;form&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;connection&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;supports&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;many&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;many&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;products&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;call&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;hibernate.cfg.xml&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Set&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;its&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Copy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Output&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Copy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;always&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Server&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Compact&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Edition&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;enter&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;information&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;into&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;xml&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;?&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Dialect.MsSqlCeDialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Driver.SqlServerCeDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Data&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Source=FirstSample.sdf&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;With&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tell&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;MS&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Server&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Compact&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Edition&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;target&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;shall&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample.sdf&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=connection&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;string)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defined&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;see&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;SQL&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;generates&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sends&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;highly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;recommended&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;debugging&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;purposes&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;during&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;development)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Double&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;check&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;no&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;typos&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;empty&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample.sdf&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;choose&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Local&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Database&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;template&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Click&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ignore&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;DataSet&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;creation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;wizard&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;hit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Cancel)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Setup&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;It&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;setup&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;First&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;verify&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SharedLibs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;last&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;8&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Microsoft&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Server&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Compact&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Edition&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;directory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Programs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Note&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;System.Data.SqlServerCe.dll&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;located&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sub-folder&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Desktop&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;All&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;other&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;found&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reference&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Additionally&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;references&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.dll&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;nunit.framework.dll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Systm.Data.SqlServerCe.dll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;remember&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reference&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;located&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SharedLibs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder!)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Pay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;attention&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Copy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Local&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;true&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assembly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;System.Data.SqlServerCe.dll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;default&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;copy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;hibernate.cfg.xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;root&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;unit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Direct&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;action&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NUnit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;needs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;access&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;GenerateSchema_Fixture&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;look&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;like&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;further&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;need&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;7&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sqce*.dll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;output&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;directory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;post-build&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;VS&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Enter&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;command&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Post-build&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;command&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;line&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;copy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;$&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(ProjectDir)..\..\SharedLibs\sqlce*.dll&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;$&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(ProjectDir)$(OutDir)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GenerateSchema_Fixture&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Cfg&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Tool.hbm2ddl&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NUnit.Framework&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Tests&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;TestFixture]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GenerateSchema_Fixture&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_generate_schema(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cfg&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Configuration()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;cfg.Configure()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;cfg.AddAssembly&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;typeof&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product).Assembly);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SchemaExport(cfg).Execute&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;line&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;creates&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;instance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;used&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;second&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;line&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tell&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;itself&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;look&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;out&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;information&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;provide&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;any&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;information&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;So&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;search&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;called&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate.cfg.xml&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;output&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;directory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;That&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;exactly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;what&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defined&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;settings&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;third&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;line&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tell&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;information&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assembly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contains&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;At&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;being&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;such&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product.hbm.xml)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;embedded&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;resource&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fourth&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;line&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;uses&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SchemaExport&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;helper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;auto&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;magically&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;generate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;schema&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;us&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SchemaExport&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;each&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;call&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;delete&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;data&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;recreate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Note&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NOT&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;out&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;whether&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;does&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;its&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;job&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;correctly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;does&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;rater&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;whether&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;setup&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;system&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;correctly&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;However&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;check&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;see&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;newly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;created&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;If&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;a href="http://www.testdriven.net/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;TestDriven.Net&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;installed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;right&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;click&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inside&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;choose&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Run&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Test(s&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;execute&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;If&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;every&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;thing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ok&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;see&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;result&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;output&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;window&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;If&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;a href="http://www.jetbrains.com/resharper/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ReSharper&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;installed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;clicking&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;yellow-green&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;circle&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;left&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;border&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;choose&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Run&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;result&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;case&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Problems&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;If&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fails&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;double&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;check&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;target&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;directory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Double&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;check&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;no&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;typos&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;hibernate.cfg.xml)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product.hbm.xml).&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Finally&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;check&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;whether&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Build&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Action&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product.hbm.xml)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Embedded&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Resource&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;continue&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;succeeds&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;CRUD&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;operations&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;obviously&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;system&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ready&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;successfully&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implemented&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Domain&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defined&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configured&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Finally&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;used&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;automatically&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;generate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;schema&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;files)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;spirit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;DDD&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;see&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;e.g&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;a href="http://domaindrivendesign.org/books/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Driven&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Design&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Eric&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Evans&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;crud&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;operations&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;create,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;read&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;update&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;delete)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;interface&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;part&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;where&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implementation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implementation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;infrastructure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;specific&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;keep&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;persistence&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ignorant&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;PI).&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;interface&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Call&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;interface&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;System&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;System.Collections.Generic&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;interface&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Remove(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetById(Guid&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;productId)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByName&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;ICollection&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByCategory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;category)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository_Fixture&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;TestFixture]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository_Fixture&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ISessionFactory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;configuration;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;TestFixtureSetUp]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;TestFixtureSetUp(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Configuration()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;configuration.Configure();&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;configuration.AddAssembly(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;typeof&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product).Assembly);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;configuration.BuildSessionFactory();&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fourth&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;line&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;TestFixtureSetUp&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;factory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;expensive&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;process&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;thus&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;executed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;once&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;That&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reason&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;why&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;put&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;into&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;executed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;once&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;during&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cycle&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;To&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;keep&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;methods&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;side&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;effect&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;free&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;re-create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;schema&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;before&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;execution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;each&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Thus&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;SetUp]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SetupContext(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SchemaExport(_configuration).Execute&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;And&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;instance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;adding&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;called&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Repositories&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;project&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;folder&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Make&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inherit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;interface&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;System&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;System.Collections.Generic&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Repositories&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Remove(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetById(Guid&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;productId&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByName&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ICollection&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByCategory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;category&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;throw&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NotImplementedException()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Manipulating&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Data&lt;/span&gt;&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;go&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;back&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository_Fixture&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_add_new_product(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;repository.Add(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;run&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fail&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;yet&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implemented&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;But&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;wait&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;little&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;helper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;provides&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;us&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;objects&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demand&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Domain&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Cfg&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;namespace&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FirstSolution.Repositories&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateHelper&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ISessionFactory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ISessionFactory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SessionFactory&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;_sessionFactory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Configuration()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;configuration.Configure()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;configuration.AddAssembly&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;typeof&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product).Assembly);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configuration.BuildSessionFactory()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;static&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OpenSession(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SessionFactory.OpenSession()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;creates&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;factory&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;client&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;needs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ITransaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;transaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;session.Save(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;second&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;run&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;again&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fail&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;message&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;That&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;because&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;default&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configured&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt;&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;load&lt;/span&gt;&lt;/a&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entities&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;That&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;recommended&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;approach&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;warmly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;recommend&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;change&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;maximum&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;flexibility&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;How&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solve&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;issue&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;?&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;It&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;easy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;make&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;properties&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;methods&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object(s&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;virtual&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt;&lt;br /&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Guid&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Id&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;virtual&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Discontinued&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;set&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;run&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;again&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;It&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;succeed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;output&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Note&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sql&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;spit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;out&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;think&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;successfully&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inserted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;into&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;But&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;whether&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;really&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;so&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;extend&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;our&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_add_new_product(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;repository.Add(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;try&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;load&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.Get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;(product.Id);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;successfully&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inserted&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.IsNotNull(fromDb)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreNotSame(product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreEqual(product.Name&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb.Name)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreEqual(product.Category&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb.Category)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Run&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;again&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Hopefully&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;succeed..&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ready&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;other&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;methods&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;For&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;testing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;would&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;rather&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;table&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;already&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;containing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;some&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;products&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nothing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;easier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;than&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;CreateInitialData&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;readonly&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;]&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;products&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Coca&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Cola&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Pepsi&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Cola&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Category&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;CreateInitialData(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ITransaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;transaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;foreach&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;products)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        &lt;span style="text-decoration: line-through; color: red;"&gt;session.Save(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Call&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SetupContext&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;after&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;create&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;schema&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;call&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;done&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;each&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;after&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;schema&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;created&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;populated&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;some&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;products&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_update_existing_product(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;products[0];&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;product.Name&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Yellow&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Pear&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;repository.Update(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;try&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;load&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.Get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;(product.Id);&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreEqual(product.Name&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb.Name)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;When&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;running&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;will&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fail&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;has&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;yet&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;been&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implemented&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Note&lt;/span&gt;&lt;/b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;expected&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;behavior&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;since&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;TDD&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;run&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;always&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fail&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Analogous&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;difference&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;call&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;update&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;instead&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;save&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ITransaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;transaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;session.Update(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Run&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;again&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;watch&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;it&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;succeed&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;delete&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;straight&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;forward&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;When&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;testing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;whether&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;record&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;has&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;really&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;been&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;deleted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assert&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;returned&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;equal&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;null&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Here&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_remove_existing_product(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;products[0];&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;repository.Remove(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;_&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;sessionFactory.OpenSession())&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.Get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;(product.Id);&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.IsNull(fromDb)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implementation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Remove&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Remove(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ITransaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;transaction&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.BeginTransaction()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;session.Delete(product)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;transaction.Commit()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Querying&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Database&lt;/span&gt;&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;We&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;still&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;three&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;methods&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;objects&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;most&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;easy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetById&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;First&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;write&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_get_existing_product_by_id(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository.GetById(_products[1].Id)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.IsNotNull(fromDb)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreNotSame(_products[1]&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreEqual(_products[1].Name&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb.Name)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;then&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fulfill&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetById(Guid&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;productId&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session.Get&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;(productId);&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Now&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;easy&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;For&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;two&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;methods&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByName&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;As&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;usual&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;we&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;write&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_get_existing_product_by_name(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository.GetByName(_products[1].Name)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.IsNotNull(fromDb)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreNotSame(_products[1]&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreEqual(_products[1].Id&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb.Id)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implementation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByName&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;done&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;two&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;different&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;approaches&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;first&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Hibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Query&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Language&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;second&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HCQ&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Hibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Criteria&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Query)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Let&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;start&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HQL&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;oriented&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;language&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;similar&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;equal&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SQL&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;To&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;added&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implemetation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByName&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HQL&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HCQ&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;below&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;works&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;expected&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;returns&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;above&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;introduced&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;commonly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;used&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;technique&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;when&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;It&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;called&lt;/span&gt; &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;fluent&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;interfaces&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;As&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;result&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;less&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;verbose&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;easier&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;understand&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;You&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;see&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;embedded&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;named)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;parameters&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Parameters&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;prefixed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;defines&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;many&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;helper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;methods&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;like&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SetString&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;used&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;example&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assign&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;values&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;various&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;types&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;those&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;parameters&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Finally&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;UniqueResult&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tell&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;expect&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;record&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;If&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;more&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;than&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;one&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;record&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;returned&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;then&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;exception&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;raised&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;To&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;more&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;information&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;about&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;please&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;read&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;online&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;second&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;uses&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;criteria&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;search&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;requested&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;You&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;need&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;add&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reference&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate.Criterion&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;page&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByName&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;CreateCriteria(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;typeof&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product))&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add(Restrictions.Eq(&lt;/span&gt;&lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;UniqueResult&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;();&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Many&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;users&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;think&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;approach&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;more&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;object&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;oriented&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;On&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;other&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;hand&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;complex&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;written&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;criteria&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;syntax&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;quickly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;become&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;difficult&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;understand&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;last&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByCategory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;returns&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;list&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;products&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implemented&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;follows&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Test]&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;void&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Can_get_existing_products_by_category(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;IProductRepository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;new&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ProductRepository()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;repository.GetByCategory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.AreEqual(2&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb.Count)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.IsTrue(IsInCollection(_products[0]&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb))&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;Assert.IsTrue(IsInCollection(_products[1]&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb))&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;private&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;bool&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IsInCollection(Product&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;product&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ICollection&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;foreach&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;item&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;fromDb&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;product.Id&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;item.Id&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;method&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;itself&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;might&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;following&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt;&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;public&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ICollection&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;GetByCategory&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;category&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ISession&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernateHelper.OpenSession()&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="text-decoration: line-through; color: red;"&gt;var&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;products&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;session&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;CreateCriteria(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;typeof&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Product))&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Add(Restrictions.Eq(&lt;/span&gt;&lt;span style="color:#006080;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;category)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;br /&gt;                    &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;List&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;Product&amp;gt;();&lt;/span&gt;&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;return&lt;/span&gt;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;products&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="text-decoration: line-through; color: red;"&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Summary&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;In&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;article&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;shown&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;how&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;implement&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;basic&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;define&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mapping&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;how&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configure&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;able&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;persist&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;objects&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;shown&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;how&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;typically&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;write&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;test&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;CRUD&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;methods&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;objects&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;taken&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;MS&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;SQL&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Compact&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Edition&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sample&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;any&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;other&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;supported&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;used&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;change&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;hibernate.cfg.xml&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;file&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;accordingly)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Ee&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;no&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;dependencies&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;external&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;frameworks&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;tools&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;other&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;than&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;itself&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.NET&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;course&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;never&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;counts&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;here)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;p&gt;&lt;a href="http://www.dicapriophotography.com"&gt;&lt;span style="background: SpringGreen;"&gt;san&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;diego&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;wedding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;photographer&lt;/span&gt;&lt;/a&gt;
&lt;a href="http://www.dicapriophotography.com"&gt;&lt;span style="background: SpringGreen;"&gt;san&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;diego&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;wedding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;photographers&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/10.aspx</link><pubDate>Thu, 18 Jun 2009 16:56:43 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:299</guid><dc:creator>SChalk</dc:creator><description>Revision 10 posted to How to by SChalk on 18/06/2009 01:56:43 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called &amp;nbsp; &lt;code&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;c:\Code\Libraries\NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;SharedLibs&lt;/span&gt;&amp;nbsp; &lt;code&gt;&lt;span style="background: SpringGreen;"&gt;c:\Code\SharedLibs\NHibernate&lt;/span&gt;&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with. This is your SharedLib folder from which you need to add your references to the NHibernate and NUnit dlls. Add references to NHibernate to both the demo project and the unit test project.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project. Note this code is dependent on Visual Studio 2008 and .Net Framework 3.5.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain). You will have added the design folder and created the class diagram yourself although this is for good practice and not required for the purposes of this excercise.&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false! Add a copy of hibernate.cfg.xml to the root of this unit test project. Direct action with NHibernate in the NUnit project needs access to this file.&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us. SchemaExport will create the product table in the database and each time you call it it will delete the table and the table data and recreate it.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly. However, you can check the database and see the newly created &amp;#39;product&amp;#39; table.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cycle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;To be added: implemetation of GetByName using HQL. Implement HCQ as below this works as expected and returns a product entity.&lt;/p&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reference&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;NHibernate.Criterion&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;page&lt;/span&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/9.aspx</link><pubDate>Thu, 18 Jun 2009 16:50:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:285</guid><dc:creator>SChalk</dc:creator><description>Revision 9 posted to How to by SChalk on 18/06/2009 01:50:15 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called&amp;nbsp; &lt;code&gt;c:\Code\Libraries\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with. This is your SharedLib folder from which you need to add your references to the NHibernate and NUnit dlls. Add references to NHibernate to both the demo project and the unit test project.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project. Note this code is dependent on Visual Studio 2008 and .Net Framework 3.5.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain). You will have added the design folder and created the class diagram yourself although this is for good practice and not required for the purposes of this excercise.&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false! Add a copy of hibernate.cfg.xml to the root of this unit test project. Direct action with NHibernate in the NUnit project needs access to this file.&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us. SchemaExport will create the product table in the database and each time you call it it will delete the table and the table data and recreate it.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly. However, you can check the database and see the newly created &amp;#39;product&amp;#39; table.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test &lt;span style="text-decoration: line-through; color: red;"&gt;cicle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;cycle&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;To&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;added&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implemetation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;GetByName&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HQL&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HCQ&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;below&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;works&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expected&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;returns&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/8.aspx</link><pubDate>Thu, 18 Jun 2009 16:45:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:284</guid><dc:creator>SChalk</dc:creator><description>Revision 8 posted to How to by SChalk on 18/06/2009 01:45:17 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called&amp;nbsp; &lt;code&gt;c:\Code\Libraries\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with. &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SharedLib&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;references&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NUnit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dlls&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;references&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;both&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demo&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Note&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dependent&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Visual&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Studio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;2008&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Net&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Framework&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3.5&lt;/span&gt;.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple &lt;span style="text-decoration: line-through; color: red;"&gt;domain&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;domain)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;added&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;design&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;created&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;diagram&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;yourself&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;although&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;good&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;practice&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;required&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;purposes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;excercise&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false! Add a &lt;span style="background: SpringGreen;"&gt;copy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hibernate.cfg.xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Direct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;action&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NUnit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;needs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;access&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SchemaExport&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;call&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;delete&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;data&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recreate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;However&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;newly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;created&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;product&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cicle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/7.aspx</link><pubDate>Tue, 16 Jun 2009 19:49:01 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:283</guid><dc:creator>breynolds</dc:creator><description>Revision 7 posted to How to by breynolds on 16/06/2009 04:49:01 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called&amp;nbsp; &lt;code&gt;c:\Code\Libraries\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/div&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the database (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false!&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cicle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            get&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                 {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                 };&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;            {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/6.aspx</link><pubDate>Fri, 01 May 2009 22:00:50 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:282</guid><dc:creator>Weston Binford</dc:creator><description>Revision 6 posted to How to by Weston Binford on 01/05/2009 07:00:50 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called&amp;nbsp; &lt;code&gt;c:\Code\Libraries\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;pre&gt;&lt;div&gt;&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/p&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; &lt;br /&gt;                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the &lt;span style="text-decoration: line-through; color: red;"&gt;datbase&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt; (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false!&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cicle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;br /&gt;        {&lt;br /&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Add(product);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Save(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }&lt;br /&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;            {&lt;br /&gt;                session.Update(product);&lt;br /&gt;                transaction.Commit();&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()&lt;br /&gt;        {&lt;br /&gt;            var product = _products[0];&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            repository.Remove(product);&lt;br /&gt; &lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;br /&gt;                Assert.IsNull(fromDb);&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())&lt;br /&gt;                {&lt;br /&gt;                    session.Delete(product);&lt;br /&gt;                    transaction.Commit();&lt;br /&gt;                }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetById(_products[1].Id);&lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Name, fromDb.Name);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByName(_products[1].Name);&lt;br /&gt; &lt;br /&gt;            Assert.IsNotNull(fromDb);&lt;br /&gt;            Assert.AreNotSame(_products[1], fromDb);&lt;br /&gt;            Assert.AreEqual(_products[1].Id, fromDb.Id);&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                Product product = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))&lt;br /&gt;                    .UniqueResult&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]&lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()&lt;br /&gt;        {&lt;br /&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;br /&gt;            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);&lt;br /&gt; &lt;br /&gt;            Assert.AreEqual(2, fromDb.Count);&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[0], fromDb));&lt;br /&gt;            Assert.IsTrue(IsInCollection(_products[1], fromDb));&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)&lt;br /&gt;                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())&lt;br /&gt;            {&lt;br /&gt;                var products = session&lt;br /&gt;                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))&lt;br /&gt;                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))&lt;br /&gt;                    .List&amp;lt;Product&amp;gt;();&lt;br /&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;&lt;br /&gt;            }&lt;br /&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/5.aspx</link><pubDate>Sat, 07 Feb 2009 17:34:11 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:261</guid><dc:creator>Tobin Harris</dc:creator><description>Revision 5 posted to How to by Tobin Harris on 07/02/2009 02:34:11 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is what we&amp;#39;re aiming for:&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;But first things first [:)]&lt;/p&gt;
&lt;p&gt;Lets start by actually doing something with that ZIP file you just downloaded.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called&amp;nbsp; &lt;code&gt;c:\Code\Libraries\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;pre&gt;&lt;div&gt;&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
 
  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/p&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;
 
&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the datbase (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false!&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cicle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()
        {
            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()
        {
            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Add(product);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(product);
                transaction.Commit();
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()
        {
            var product = _products[0];
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Remove(product);
 
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);
                Assert.IsNull(fromDb);
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetById(_products[1].Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Name, fromDb.Name);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByName(_products[1].Name);
 
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Id, fromDb.Id);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                Product product = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))
                    .UniqueResult&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);
 
            Assert.AreEqual(2, fromDb.Count);
            Assert.IsTrue(IsInCollection(_products[0], fromDb));
            Assert.IsTrue(IsInCollection(_products[1], fromDb));
        }
 
        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)
        {
            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)
                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)
                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                var products = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))
                    .List&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/4.aspx</link><pubDate>Sat, 07 Feb 2009 17:27:23 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:212</guid><dc:creator>Tobin Harris</dc:creator><description>Revision 4 posted to How to by Tobin Harris on 07/02/2009 02:27:23 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;Welcome to NHibernate&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;re reading this, we assume that you&amp;#39;ve just &lt;a href="/media/p/4.aspx"&gt;downloaded NHibernate&lt;/a&gt; and want to get started using it.&lt;/p&gt;
&lt;p&gt;This tutorial will talk you through the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing NHibernate&lt;/li&gt;
&lt;li&gt;Defining a simple business object class.&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Create an NHibernate mapping to load and save the business object.&lt;/li&gt;
&lt;li&gt;Configure NHibernate to talk to your local database.&lt;/li&gt;
&lt;li&gt;Automatically generate a database&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Writing simple CRUD code using the Repository pattern.&lt;/li&gt;
&lt;li&gt;Using Unit Tests to make sure the code is working correctly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;First&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;lets&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;look&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;how&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;aiming&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;But&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;things&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:)]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Lets&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;actually&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;doing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;something&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ZIP&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; you &lt;span style="text-decoration: line-through; color: red;"&gt;install&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt;.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Installing NHibernate&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If you&amp;#39;ve downloaded the NHibernate binaries in a zip file, all you need to do is extract that file to somewhere sensible. I usually create a folder called&amp;nbsp; &lt;code&gt;c:\Code\Libraries\NHibernate&lt;/code&gt; and extract the zip to there. But whatever you&amp;#39;re comfortable with.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it! NHibernate is installed (easy huh). We&amp;#39;ll talk you through using it with Visual Studio in a moment. First let&amp;#39;s look at how we go about creating a project.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;Create Your Project&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Before we start building our application and business objects, we&amp;#39;ll need to create a blank project to put them in. Fire up Visual Studio and create a new Class Library project. Let&amp;#39;s now look at something interesting: creating a business object.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Defining the Business Objects&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;pre&gt;&lt;div&gt;&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
 
  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/p&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;
 
&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the datbase (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false!&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cicle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()
        {
            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()
        {
            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Add(product);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(product);
                transaction.Commit();
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()
        {
            var product = _products[0];
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Remove(product);
 
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);
                Assert.IsNull(fromDb);
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetById(_products[1].Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Name, fromDb.Name);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByName(_products[1].Name);
 
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Id, fromDb.Id);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                Product product = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))
                    .UniqueResult&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);
 
            Assert.AreEqual(2, fromDb.Count);
            Assert.IsTrue(IsInCollection(_products[0], fromDb));
            Assert.IsTrue(IsInCollection(_products[1], fromDb));
        }
 
        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)
        {
            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)
                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)
                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                var products = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))
                    .List&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/3.aspx</link><pubDate>Sat, 07 Feb 2009 17:15:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:211</guid><dc:creator>Tobin Harris</dc:creator><description>Revision 3 posted to How to by Tobin Harris on 07/02/2009 02:15:49 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;b&gt;Schenker&lt;/strong&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Define&lt;/span&gt; &lt;/b&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;span style="background: SpringGreen;"&gt;Welcome&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assume&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ve&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;a href="/media/p/4.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;started&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tutorial&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;talk&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;through&lt;/span&gt; the &lt;span style="text-decoration: line-through; color: red;"&gt;Domain&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Installing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Defining&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;load&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;save&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Configure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;talk&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;local&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Automatically&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CRUD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Repository&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;pattern&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Unit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Tests&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sure&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correctly&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;First&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lets&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;install&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Installing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ve&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;binaries&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;zip&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;extract&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;file&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;somewhere&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sensible&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;usually&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt;&amp;nbsp; &lt;code&gt;&lt;span style="background: SpringGreen;"&gt;c:\Code\Libraries\NHibernate&lt;/span&gt;&lt;/code&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;extract&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;zip&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;there&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;But&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whatever&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;comfortable&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;That&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;installed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;easy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;huh)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;We&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;talk&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;through&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Visual&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Studio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;moment&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;First&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;go&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;start&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;building&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;our&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;application&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;objects&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;we&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blank&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;put&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;them&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fire&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;up&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Visual&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Studio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Library&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Let&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;look&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;something&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;interesting&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;object&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Defining&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Business&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Objects&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;b&gt;Product&lt;/b&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;b&gt;Product.cs&lt;/b&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;b&gt;NHibernate&lt;/b&gt;for this task. An instance of an entity in the domain corresponds to a &lt;b&gt;row in a table&lt;/b&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Define the Mapping&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;b&gt;Mappings&lt;/b&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;b&gt;&lt;b&gt;Product&lt;/b&gt;.hbm.xml&lt;/b&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;b&gt;Build Action&lt;/b&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;pre&gt;&lt;div&gt;&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
 
  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;b&gt;assembly&lt;/b&gt; and &lt;b&gt;namespace&lt;/b&gt; of the root node. It&amp;#39;s similar to the &lt;b&gt;using&lt;/b&gt; statement in C#.&lt;/p&gt;
&lt;p&gt;Now we have to first define a &lt;b&gt;primary key&lt;/b&gt; for the product entity. Technically we could take the property &lt;b&gt;Name&lt;/b&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;b&gt;Id&lt;/b&gt;. We use &lt;b&gt;Guid&lt;/b&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;
 
&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;b&gt;Domain.cd&lt;/b&gt; contains the class diagram of our simple domain)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Configure NHibernate&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;b&gt;Copy to Output&lt;/b&gt;&amp;quot; to &amp;quot;&lt;b&gt;Copy always&lt;/b&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;b&gt;SQL&lt;/b&gt; NHibernate generates and sends to the datbase (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;b&gt;FirstSample.sdf&lt;/b&gt; to the FirstSample project (choose &lt;b&gt;Local Database&lt;/b&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Test the Setup&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;b&gt;Microsoft SQL Server Compact Edition&lt;/b&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;b&gt;System.Data.SqlServerCe.dll&lt;/b&gt; is located in the sub-folder &lt;b&gt;Desktop&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;b&gt;Copy Local&lt;/b&gt;&amp;quot; to &lt;b&gt;true&lt;/b&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false!&lt;/p&gt;
&lt;p&gt;Add a class called &lt;b&gt;GenerateSchema_Fixture&lt;/b&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:370px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;b&gt;hibernate.cfg.xml&lt;/b&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;b&gt;NOT&lt;/b&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;b&gt;Run Test(s)&lt;/b&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;b&gt;Run&lt;/b&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;In case of Problems&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;b&gt;Build Action&lt;/b&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;b&gt;Embedded Resource&lt;/b&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Our first CRUD operations&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:270px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cicle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;[SetUp]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()
        {
            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;b&gt;FirstSolution&lt;/b&gt; project. Add a class &lt;b&gt;ProductRepository&lt;/b&gt; to this folder. Make the&lt;b&gt;ProductRepository&lt;/b&gt; inherit from the interface &lt;b&gt;IProductRepository&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:652px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Manipulating Data&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;      [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()
        {
            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Add(product);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;b&gt;Add&lt;/b&gt; method in the repository class. Let&amp;#39;s do it. &lt;b&gt;But wait&lt;/b&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:523px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            get&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;b&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/b&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:313px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:355px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                 };&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:269px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;            {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;            }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;b&gt;Note&lt;/b&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(product);
                transaction.Commit();
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:227px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()
        {
            var product = _products[0];
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Remove(product);
 
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);
                Assert.IsNull(fromDb);
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;b&gt;Querying the Database&lt;/b&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetById(_products[1].Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Name, fromDb.Name);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByName(_products[1].Name);
 
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Id, fromDb.Id);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                Product product = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))
                    .UniqueResult&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;height:314px;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);
 
            Assert.AreEqual(2, fromDb.Count);
            Assert.IsTrue(IsInCollection(_products[0], fromDb));
            Assert.IsTrue(IsInCollection(_products[1], fromDb));
        }
 
        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)
        {
            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)
                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)
                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="border:1px solid gray;margin:20px 0px 10px;padding:4px;overflow:auto;font-size:8pt;width:97.5%;cursor:text;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;max-height:200px;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                var products = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))
                    .List&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Your first NHibernate based application</title><link>http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application/revision/2.aspx</link><pubDate>Sat, 07 Feb 2009 15:32:03 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:210</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 2 posted to How to by Fabio Maulo on 07/02/2009 12:32:03 p.m.&lt;br /&gt;
&lt;h2&gt;Your first NHibernate based application&lt;/h2&gt;
&lt;h4&gt;&lt;strong&gt;&lt;span style="color:#ff8000;"&gt;Wiki extracted from the original blog post of&lt;/span&gt;&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;a href="/members/gabriel.schenker/default.aspx"&gt;Gabriel&amp;nbsp;&lt;span style="font-weight:normal;"&gt;&lt;strong&gt;Schenker&lt;/strong&gt;&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;h4&gt;&lt;strong&gt;Define the Domain&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Lets start by defining a very simple domain. For the moment it consists of one entity called &lt;strong&gt;Product&lt;/strong&gt;. The product has 3 properties Name, Category and Discontinued.&lt;/p&gt;
&lt;p style="padding-left:210px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ProductDef.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Add a folder Domain to the FirstSample project of your solution. Add a new class &lt;strong&gt;Product.cs&lt;/strong&gt; to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler) &lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we want to be able to persist instances of this entity in a (relational) database. We have chosen &lt;strong&gt;NHibernate&lt;/strong&gt;for this task. An instance of an entity in the domain corresponds to a &lt;strong&gt;row in a table&lt;/strong&gt; in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I&amp;#39;ll start with the mapping file.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Define the Mapping&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Create a folder &lt;strong&gt;Mappings&lt;/strong&gt; in the FirstSample project. Add a new xml-document to this folder and call it &lt;strong&gt;&lt;strong&gt;Product&lt;/strong&gt;.hbm.xml&lt;/strong&gt;. Please note the &amp;quot;hbm&amp;quot; part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define &amp;quot;Embedded Resource&amp;quot; as &lt;strong&gt;Build Action&lt;/strong&gt; for this xml file.&lt;/p&gt;
&lt;p&gt;In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.&lt;/p&gt;
&lt;p&gt;Back in VS add the schema to the Product.hbm.xml file&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/properties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s start now. Each mapping file has to define a &amp;lt;hibernate-mapping&amp;gt; root node&lt;/p&gt;
&lt;div id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:73647d35-a397-46d8-ad1c-a9edf8bbe509" class="wlWriterSmartContent"&gt;
&lt;pre&gt;&lt;div&gt;&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
 
  &lt;span style="color:#008000;"&gt;&amp;lt;!-- more mapping info here --&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes &lt;strong&gt;assembly&lt;/strong&gt; and &lt;strong&gt;namespace&lt;/strong&gt; of the root node. It&amp;#39;s similar to the &lt;strong&gt;using&lt;/strong&gt; statement in C#.&lt;/p&gt;
&lt;p&gt;Now we have to first define a &lt;strong&gt;primary key&lt;/strong&gt; for the product entity. Technically we could take the property &lt;strong&gt;Name&lt;/strong&gt; of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it &lt;strong&gt;Id&lt;/strong&gt;. We use &lt;strong&gt;Guid&lt;/strong&gt; as the type of the Id but it can as well be an int or a long.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;
 
&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain
{
    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The complete mapping file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution&amp;quot;&lt;/span&gt; 
                   &lt;span style="color:#ff0000;"&gt;namespace&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;FirstSolution.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
      &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;guid&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Category&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Discontinued&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;NHibernate doesn&amp;#39;t get in our way such as that it defines many reasonable defaults. So if you don&amp;#39;t provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/mapping.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Your solution explorer should look like this now (&lt;strong&gt;Domain.cd&lt;/strong&gt; contains the class diagram of our simple domain)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Configure NHibernate&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!&lt;/p&gt;
&lt;p&gt;Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property &amp;quot;&lt;strong&gt;Copy to Output&lt;/strong&gt;&amp;quot; to &amp;quot;&lt;strong&gt;Copy always&lt;/strong&gt;&amp;quot;. Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#800000;"&gt;xml&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Connection.DriverConnectionProvider&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Dialect.MsSqlCeDialect&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;NHibernate.Driver.SqlServerCeDriver&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;Data Source=FirstSample.sdf&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
    
    &lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
  &lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the &lt;strong&gt;SQL&lt;/strong&gt; NHibernate generates and sends to the datbase (highly recommended for debugging purposes during development). Double check that you have no typos in the code!&lt;/p&gt;
&lt;p&gt;Add an empty database called &lt;strong&gt;FirstSample.sdf&lt;/strong&gt; to the FirstSample project (choose &lt;strong&gt;Local Database&lt;/strong&gt; as template)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AddNewItem.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Click Add and ignore the DataSet creation wizard (just hit Cancel).&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Test the Setup&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;It&amp;#39;s now time to test our setup. First verify that you have the following files in your SharedLibs folder&lt;/p&gt;
&lt;p style="padding-left:180px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/SharedLibs.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The last 8 files you can find in the &amp;quot;&lt;strong&gt;Microsoft SQL Server Compact Edition&lt;/strong&gt;&amp;quot; directory in your Programs folder.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/sqlce.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note: the &lt;strong&gt;System.Data.SqlServerCe.dll&lt;/strong&gt; is located in the sub-folder &lt;strong&gt;Desktop&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;All other files can be found in the NHibernate folder&lt;/p&gt;
&lt;p&gt;Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property &amp;quot;&lt;strong&gt;Copy Local&lt;/strong&gt;&amp;quot; to &lt;strong&gt;true&lt;/strong&gt; for the assembly System.Data.SqlServerCe.dll since by default it is set to false!&lt;/p&gt;
&lt;p&gt;Add a class called &lt;strong&gt;GenerateSchema_Fixture&lt;/strong&gt; to your test project. Your test project should now look like this&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/solutionex1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the &amp;quot;Post-build event command line&amp;quot;&lt;/p&gt;
&lt;p&gt;copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/AppProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Now add the following code to the GenerateSchema_Fixture file&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:370px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Tool.hbm2ddl;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Tests&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    [TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; GenerateSchema_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_generate_schema()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var cfg = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            cfg.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            cfg.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(cfg).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called &lt;strong&gt;hibernate.cfg.xml&lt;/strong&gt; in the output directory. That&amp;#39;s exactly what we want since we have defined our settings in this file.&lt;/p&gt;
&lt;p&gt;In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.&lt;/p&gt;
&lt;p&gt;The fourth line of code uses the SchemaExport helper class of NHibernate to auto-&amp;quot;magically&amp;quot; generate the schema in the database for us.&lt;/p&gt;
&lt;p&gt;Note: with this test method we do &lt;strong&gt;NOT&lt;/strong&gt; want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly.&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.testdriven.net/"&gt;TestDriven.Net&lt;/a&gt; installed you can now just right click inside the test method and choose &amp;quot;&lt;strong&gt;Run Test(s)&lt;/strong&gt;&amp;quot; to execute the test.&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If every thing is ok you should see the following result in the output window&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestResult.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If you have &lt;a href="http://www.jetbrains.com/resharper/"&gt;ReSharper&lt;/a&gt; installed you can just start the test by clicking the yellow-green circle on the left border and choose &lt;strong&gt;Run&lt;/strong&gt;.&lt;/p&gt;
&lt;p style="padding-left:120px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSTestinfo.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The result is as follows&lt;/p&gt;
&lt;p style="padding-left:90px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/RSRunTest.png" alt="" /&gt;&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;In case of Problems&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)&lt;/p&gt;
&lt;p style="padding-left:150px;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/InCaseOfPb.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the &amp;quot;&lt;strong&gt;Build Action&lt;/strong&gt;&amp;quot; of the mapping file (Product.hbm.xml) to &amp;quot;&lt;strong&gt;Embedded Resource&lt;/strong&gt;&amp;quot;. Only continue if the test succeeds.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Our first CRUD operations&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).&lt;/p&gt;
&lt;p&gt;In the spirit of DDD (see e.g. &lt;a href="http://domaindrivendesign.org/books/"&gt;Domain Driven Design&lt;/a&gt; by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).&lt;/p&gt;
&lt;p&gt;Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let&amp;#39;s define the following interface&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:269px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Domain&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; IProductRepository&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        Product GetById(Guid productId);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Add a class ProductRepository_Fixture to the test project of the solution and add the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:270px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;[TestFixture]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository_Fixture&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Configuration _configuration;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [TestFixtureSetUp]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; TestFixtureSetUp()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            _configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Product).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            _sessionFactory = _configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That&amp;#39;s the reason why I put it into this method which is only executed once during a test cicle.&lt;/p&gt;
&lt;p&gt;To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;[SetUp]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; SetupContext()
        {
            &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; SchemaExport(_configuration).Execute(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your &lt;strong&gt;FirstSolution&lt;/strong&gt; project. Add a class &lt;strong&gt;ProductRepository&lt;/strong&gt; to this folder. Make the&lt;strong&gt;ProductRepository&lt;/strong&gt; inherit from the interface &lt;strong&gt;IProductRepository&lt;/strong&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:652px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; ProductRepository : IProductRepository&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;throw&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;strong&gt;Manipulating Data&lt;/strong&gt;&lt;/h6&gt;
&lt;p&gt;Now go back to the ProductRepository_Fixture test class and implement the first test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;      [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()
        {
            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Add(product);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The first run of the test method will fail since we have not yet implemented the &lt;strong&gt;Add&lt;/strong&gt; method in the repository class. Let&amp;#39;s do it. &lt;strong&gt;But wait&lt;/strong&gt;, we have to define a little helper class first which provides us session objects on demand.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:523px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; FirstSolution.Domain;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; NHibernate.Cfg;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;namespace&lt;/span&gt; FirstSolution.Repositories&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;{&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; NHibernateHelper&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISessionFactory SessionFactory&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            get&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(_sessionFactory == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                    var configuration = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Configuration();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    configuration.Configure();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                    configuration.AddAssembly(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product).Assembly);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    _sessionFactory = configuration.BuildSessionFactory();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _sessionFactory;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; SessionFactory.OpenSession();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This class creates a session factory only the first time a client needs a new session.&lt;/p&gt;
&lt;p&gt;Now we can define the Add method in the ProductRepository as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Add(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The second run of the test method will again fail with the following message&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/ShouldBeVirtual.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s because NHibernate is by default configured to use&lt;strong&gt;&amp;nbsp;&lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html"&gt;lazy load&lt;/a&gt;&lt;/strong&gt; for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.&lt;/p&gt;
&lt;p&gt;How can we solve this issue? It&amp;#39;s easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let&amp;#39;s do this for our Product class&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; Product
    {
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Name { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; Category { get; set; }
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Discontinued { get; set; }
    }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now run the test again. It should succeed and we get the following output&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestOutput.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Note the sql spit out by NHibernate.&lt;/p&gt;
&lt;p&gt;Now we think that we have successfully inserted a new product into the database. But let&amp;#39;s test it whether it is really so. Let&amp;#39;s extend our test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:313px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_add_new_product()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var product = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;};&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            repository.Add(product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                &lt;span style="color:#008000;"&gt;// Test that the product was successfully inserted&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                Assert.IsNotNull(fromDb);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                Assert.AreNotSame(product, fromDb);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                Assert.AreEqual(product.Category, fromDb.Category);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again. Hopefully it will succeed...&lt;/p&gt;
&lt;p&gt;Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:355px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; Product[] _products = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;[]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                 {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Melon&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pear&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Milk&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Coca Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                     &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Product {Name = &lt;span style="color:#006080;"&gt;&amp;quot;Pepsi Cola&amp;quot;&lt;/span&gt;, Category = &lt;span style="color:#006080;"&gt;&amp;quot;Beverages&amp;quot;&lt;/span&gt;},&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                 };&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; CreateInitialData()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt;(ITransaction transaction = session.BeginTransaction())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var product &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; _products)&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                        session.Save(product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                    transaction.Commit();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s test the Update method of the repository with the following code&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:269px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        [Test]&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_update_existing_product()&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            var product = _products[0];&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            product.Name = &lt;span style="color:#006080;"&gt;&amp;quot;Yellow Pear&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            repository.Update(product);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; &lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            &lt;span style="color:#008000;"&gt;// use session to try to load the product&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;            {&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;                Assert.AreEqual(product.Name, fromDb.Name);&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;            }&lt;/pre&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:white;border-style:none;padding:0px;"&gt;        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When running for the first time this code will fail since the Update method has not yet been implemented in the repository. &lt;strong&gt;Note&lt;/strong&gt;: This is the expected behavior since in TDD the first time you run a test it should always fail!&lt;/p&gt;
&lt;p&gt;Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Update(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(product);
                transaction.Commit();
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Run the test again an watch it succeed.&lt;/p&gt;
&lt;p&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/TestSucceed.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session&amp;#39;s get method is equal to null. Here is the test method&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:227px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_remove_existing_product()
        {
            var product = _products[0];
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            repository.Remove(product);
 
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get&amp;lt;Product&amp;gt;(product.Id);
                Assert.IsNull(fromDb);
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and here the implementation of the Remove method in the repository&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Remove(Product product)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(product);
                    transaction.Commit();
                }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h6&gt;&lt;strong&gt;Querying the Database&lt;/strong&gt;&lt;/h6&gt;
&lt;p&gt;We still have to implement the three methods which query the database for objects. Let&amp;#39;s start with the most easy one, the GetById. First we write the test&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_id()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetById(_products[1].Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Name, fromDb.Name);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and then the code to fulfill the test&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetById(Guid productId)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; session.Get&amp;lt;Product&amp;gt;(productId);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now that was easy. For the following two methods we use a new method of the session object. Let&amp;#39;s start with the GetByName method. As usual we write the test first&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;       [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_product_by_name()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByName(_products[1].Name);
 
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Id, fromDb.Id);
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let&amp;#39;s start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the above sample I have introduced a commonly used technique when using NHibernate. It&amp;#39;s called &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;fluent interfaces&lt;/a&gt;. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a &amp;#39;:&amp;#39;. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html"&gt;online documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The second version uses a criteria query to search the requested product.&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; Product GetByName(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                Product product = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, name))
                    .UniqueResult&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; product;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.&lt;/p&gt;
&lt;p&gt;The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows&lt;/p&gt;
&lt;div&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;height:314px;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        [Test]
        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Can_get_existing_products_by_category()
        {
            IProductRepository repository = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; ProductRepository();
            var fromDb = repository.GetByCategory(&lt;span style="color:#006080;"&gt;&amp;quot;Fruits&amp;quot;&lt;/span&gt;);
 
            Assert.AreEqual(2, fromDb.Count);
            Assert.IsTrue(IsInCollection(_products[0], fromDb));
            Assert.IsTrue(IsInCollection(_products[1], fromDb));
        }
 
        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; IsInCollection(Product product, ICollection&amp;lt;Product&amp;gt; fromDb)
        {
            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (var item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; fromDb)
                &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (product.Id == item.Id)
                    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;;
            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;and the method itself might contain the following code&lt;/p&gt;
&lt;div style="font-size:8pt;margin:20px 0px 10px;overflow:auto;width:97.5%;cursor:text;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;max-height:200px;border:gray 1px solid;padding:4px;"&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; ICollection&amp;lt;Product&amp;gt; GetByCategory(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; category)
        {
            &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = NHibernateHelper.OpenSession())
            {
                var products = session
                    .CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt;(Product))
                    .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Category&amp;quot;&lt;/span&gt;, category))
                    .List&amp;lt;Product&amp;gt;();
                &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; products;
            }
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).&lt;/p&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/0.aspx</link><pubDate>Fri, 01 Apr 2011 20:43:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:54</guid><dc:creator>Groxx</dc:creator><description>Current revision posted to How to by Groxx on 01/04/2011 05:43:54 p.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;The things you should care about are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; the length of the
parameter must always be the correct one in order to reuse the cached execution plans&lt;/li&gt;
&lt;li&gt;the type ot the parameter must be the same of the table&amp;#39;s column &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/6.aspx</link><pubDate>Wed, 30 Mar 2011 11:25:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:388</guid><dc:creator>markweee</dc:creator><description>Revision 6 posted to How to by markweee on 30/03/2011 08:25:54 a.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone% &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?  &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;The things you should care about are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; the length of the
parameter must always be the correct one in order to reuse the cached execution plans&lt;/li&gt;
&lt;li&gt;the type ot the parameter must be the same of the table&amp;#39;s column &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/5.aspx</link><pubDate>Tue, 17 Mar 2009 13:27:44 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:375</guid><dc:creator>Claudio Maccari</dc:creator><description>Revision 5 posted to How to by Claudio Maccari on 17/03/2009 10:27:44 a.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;The things you should care about are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; the length of the
parameter must always be the correct one in order to reuse the cached execution plans&lt;/li&gt;
&lt;li&gt;the type ot the parameter must be the same of the table&amp;#39;s column &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/4.aspx</link><pubDate>Tue, 17 Mar 2009 13:16:40 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:252</guid><dc:creator>Claudio Maccari</dc:creator><description>Revision 4 posted to How to by Claudio Maccari on 17/03/2009 10:16:40 a.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;things&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;care&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;length&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;parameter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;always&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;order&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reuse&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cached&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;execution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;plans&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;parameter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/3.aspx</link><pubDate>Mon, 16 Mar 2009 20:10:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:251</guid><dc:creator>Claudio Maccari</dc:creator><description>Revision 3 posted to How to by Claudio Maccari on 16/03/2009 05:10:22 p.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)
		.List();
}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;

exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%
&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();
}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/2.aspx</link><pubDate>Mon, 16 Mar 2009 20:10:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:250</guid><dc:creator>Claudio Maccari</dc:creator><description>Revision 2 posted to How to by Claudio Maccari on 16/03/2009 05:10:22 p.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)
		.List();
}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;

exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%
&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();
}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/1.aspx</link><pubDate>Mon, 16 Mar 2009 20:09:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:249</guid><dc:creator>Claudio Maccari</dc:creator><description>Revision 1 posted to How to by Claudio Maccari on 16/03/2009 05:09:12 p.m.&lt;br /&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)
		.List();
}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;

exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%
&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();
}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;</description></item><item><title>HiLo Generator With Node Suffix</title><link>http://nhforge.org/wikis/howtonh/hilo-generator-with-node-suffix/revision/0.aspx</link><pubDate>Thu, 31 Mar 2011 15:48:33 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:88</guid><dc:creator>Ricardo Peres</dc:creator><description>Current revision posted to How to by Ricardo Peres on 31/03/2011 12:48:33 p.m.&lt;br /&gt;
&lt;p&gt;I had recently the need for using an id generator with two requirements:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is DB-independent&lt;/li&gt;
&lt;li&gt;Allows data generated in different DBs to be merged into a centralized DB&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I already knew about HiLo generator, and it seemed fine, except that the generated ids, on different sites, would most probably clash, unless I would setup a particular configuration leaving lots of room, and would constantly monitor its usage. So I decided to adapt it: I basically pick the generator produced by the HiLo generator, and add my own suffix to it, where the suffix is different for each site. Very simple, here it is:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;public class CustomIdGenerator : TableHiLoGenerator
{
	public const String NodeSuffixAppSettingsKey = &amp;quot;NodeSuffix&amp;quot;;
 
	protected static Int32 nodeSuffix&amp;nbsp; = 0;
 
	static CustomIdGenerator()
	{
	 	//try to read the node suffix from the application settings
		if (Int32.TryParse(ConfigurationManager.AppSettings[NodeSuffixAppSettingsKey],  out nodeSuffix) == false)
		{
			//the node suffix key does not exist or is not a number
			throw new ApplicationException(String.Format(&amp;quot;Application settings does not contain key {0} or it is not a number.&amp;quot;, NodeSuffixAppSettingsKey));
		}
	}
 
	public override Object Generate(ISessionImplementor session, Object obj)
	{
		//1 - get id using standard HiLo algorithm
		Int64 baseId = (Int64) base.Generate(session, obj);
 
		//2 - shift id to the left, leaving room for 3-digit node suffixes
		baseId *= 1000;
 
		&lt;span&gt;//3 - combine id with the node suffix
		baseId += nodeSuffix;

		return (baseId);
	}
}
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;It can be configured fluently:&lt;/pre&gt;
&lt;pre&gt;public class CustomerMap : ClassMap&amp;lt;Customer&amp;gt;
{
	public CustomerMap()
	{
		this.Id(c&amp;nbsp;=&amp;gt;&amp;nbsp;c.Id).Column(&amp;quot;ID&amp;quot;).GeneratedBy.Custom&amp;lt;&lt;span&gt;CustomIdGenerator&amp;gt;();
		this.Map(c =&amp;gt; c.Name).Column(&amp;quot;NAME&amp;quot;).Not.Nullable();
		this.Map(c =&amp;gt; c.Address).Column(&amp;quot;ADDRESS&amp;quot;).Not.Nullable().LazyLoad();
		this.HasMany&amp;lt;Order&amp;gt;(c =&amp;gt; c.Orders).KeyColumn(&amp;quot;CUSTOMERID&amp;quot;).Inverse().AsSet().Cascade.All().Fetch.Join();
	}
}
&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;Or through .hbm.xml:&lt;/pre&gt;
&lt;pre&gt;&lt;pre&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/span&gt;&lt;span&gt;class&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&amp;quot;&lt;span&gt;Customer&lt;/span&gt;&amp;quot;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;table&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&amp;quot;&lt;span&gt;`CUSTOMER`&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;
&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&amp;quot;I&lt;span&gt;d&lt;/span&gt;&amp;quot;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;column&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&amp;quot;&lt;span&gt;ID&lt;/span&gt;&amp;quot;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;type&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&amp;quot;long&amp;quot;&lt;span&gt;&amp;gt;&lt;/span&gt;
&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;/span&gt;&lt;span&gt;generator&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;class&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&amp;quot;&lt;span&gt;CustomIdGenerator, CustomIdGeneratorAssembly&lt;/span&gt;&amp;quot;&lt;span&gt;&amp;gt;&lt;/span&gt;
&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/&lt;/span&gt;&lt;span&gt;generator&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;
&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;/pre&gt;</description></item><item><title>Prefent NHibernate from saving default relations</title><link>http://nhforge.org/wikis/howtonh/prefent-nhibernate-from-saving-default-relations/revision/0.aspx</link><pubDate>Wed, 30 Mar 2011 12:47:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:64</guid><dc:creator>dnperfors</dc:creator><description>Current revision posted to How to by dnperfors on 30/03/2011 09:47:53 a.m.&lt;br /&gt;
&lt;h2&gt;Prefent NHibernate from saving default relations&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;#39;s take
a look at the following code:&lt;/p&gt;
&lt;p&gt;public class TaxCalculator&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static readonly TaxCalculator
NoTax = new TaxCalculator(&amp;quot;No Tax&amp;quot;, 0.0);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public TaxCalculator()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public TaxCalculator(String
name, Double percentage)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.Name = name;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.Percentage = percentage;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Decimal CalculateTax(Decimal value)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return value * pecentage;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Guid Name { get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String Name
{ get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Decimal Percentage
{ get; set; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public
class Invoice &lt;br /&gt;
{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public Invoice()&lt;/p&gt;
&lt;p&gt;&amp;nbsp; : base(TaxCalculator.NoTax)&lt;/p&gt;
&lt;p&gt;{&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&amp;nbsp;public Invoice(TaxCalculator calculator)&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.calculator
= calculator;&lt;br /&gt;
&amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public TaxCalculator Calculator {
get; set; }&lt;br /&gt;
&amp;nbsp;public string Description
{ get; set; }&lt;br /&gt;
&amp;nbsp;public decimal Total
{ get; set; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;public decimal Tax&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get {
return Calculator.CalculateTax(Total); }&lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;In this
example I want to be able to save a reference to the TaxCalculator, using the
following mapping:&lt;/p&gt;
&lt;pre&gt;&amp;lt;class name=&amp;quot;Invoice&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;id type=&amp;quot;guid&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;guid&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Description&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Total&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;reference name=&amp;quot;Calculator&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;class name=&amp;quot;TaxCalculator&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;id name=&amp;quot;Id&amp;quot; type=&amp;quot;guid&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;guid&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Name&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Percentage&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I assume
that TaxCalculators are created before I save the invoice (that is how
we do it at the moment). The problem we are getting into is that we can&amp;#39;t save
a reference to TaxCalculator.NoTax. This is because the NoTax object will be
created over and over again and so it will not have any Id.&lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Corllins&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Woodfield&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://woodfielduniversity.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Woodfield&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&amp;nbsp;&lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The
solution could be to set the Calculator property to null, but then we have to check
for a null reference. But luckily it is also possible to let NHibernate set
this property on null when persisting the object and set it to NoTax when
loading the invoice. This is done by using Events:&lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Ashwood&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Rochville&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Adison&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;High&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;School&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;public class FlushEvent :
DefaultFlushEntityEventListener&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void OnFlushEntity(FlushEntityEvent
@event)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (@event.Entity is Invoice &amp;amp;&amp;amp; ((Invoice)@event.Entity).Calculator
== TaxCalculator.NoTax)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Invoice)@event.Entity).Calculator = null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.OnFlushEntity(@event);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class LoadedEvent
: DefaultPostLoadEventListener, IRepositoryEvent&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void OnPostLoad(PostLoadEvent
@event)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;base.OnPostLoad(@event);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (@event.Entity is Invoice &amp;amp;&amp;amp; ((Invoice)@event.Entity).Calculator
== null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Invoice)@event.Entity).Calculator = TaxCalculator.NoTax;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Prefent NHibernate from saving default relations</title><link>http://nhforge.org/wikis/howtonh/prefent-nhibernate-from-saving-default-relations/revision/2.aspx</link><pubDate>Wed, 30 Mar 2011 11:38:31 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:386</guid><dc:creator>markweee</dc:creator><description>Revision 2 posted to How to by markweee on 30/03/2011 08:38:31 a.m.&lt;br /&gt;
&lt;h2&gt;Prefent NHibernate from saving default relations&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, events, property&lt;/div&gt;

&lt;p&gt;Let&amp;#39;s take
a look at the following code:&lt;/p&gt;
&lt;p&gt;public class TaxCalculator&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static readonly TaxCalculator
NoTax = new TaxCalculator(&amp;quot;No Tax&amp;quot;, 0.0);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public TaxCalculator()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public TaxCalculator(String
name, Double percentage)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.Name = name;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.Percentage = percentage;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Decimal CalculateTax(Decimal value)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return value * pecentage;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Guid Name { get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String Name
{ get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Decimal Percentage
{ get; set; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public
class Invoice &lt;br /&gt;
{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public Invoice()&lt;/p&gt;
&lt;p&gt;&amp;nbsp; : base(TaxCalculator.NoTax)&lt;/p&gt;
&lt;p&gt;{&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&amp;nbsp;public Invoice(TaxCalculator calculator)&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.calculator
= calculator;&lt;br /&gt;
&amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public TaxCalculator Calculator {
get; set; }&lt;br /&gt;
&amp;nbsp;public string Description
{ get; set; }&lt;br /&gt;
&amp;nbsp;public decimal Total
{ get; set; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;public decimal Tax&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get {
return Calculator.CalculateTax(Total); }&lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;In this
example I want to be able to save a reference to the TaxCalculator, using the
following mapping:&lt;/p&gt;
&lt;pre&gt;&amp;lt;class name=&amp;quot;Invoice&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;id type=&amp;quot;guid&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;guid&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Description&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Total&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;reference name=&amp;quot;Calculator&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;class name=&amp;quot;TaxCalculator&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;id name=&amp;quot;Id&amp;quot; type=&amp;quot;guid&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;guid&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Name&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Percentage&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I assume
that TaxCalculators are created before I save the invoice (that is how
we do it at the moment). The problem we are getting into is that we can&amp;#39;t save
a reference to TaxCalculator.NoTax. This is because the NoTax object will be
created over and over again and so it will not have any Id. &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;Corllins&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Woodfield&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://woodfielduniversity.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Woodfield&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The
solution could be to set the Calculator property to null, but then we have to check
for a null reference. But luckily it is also possible to let NHibernate set
this property on null when persisting the object and set it to NoTax when
loading the invoice. This is done by using Events: &lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;Ashwood&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Rochville&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Adison&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;High&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;School&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;public class FlushEvent :
DefaultFlushEntityEventListener&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void OnFlushEntity(FlushEntityEvent
@event)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (@event.Entity is Invoice &amp;amp;&amp;amp; ((Invoice)@event.Entity).Calculator
== TaxCalculator.NoTax)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Invoice)@event.Entity).Calculator = null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.OnFlushEntity(@event);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class LoadedEvent
: DefaultPostLoadEventListener, IRepositoryEvent&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void OnPostLoad(PostLoadEvent
@event)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;base.OnPostLoad(@event);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (@event.Entity is Invoice &amp;amp;&amp;amp; ((Invoice)@event.Entity).Calculator
== null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Invoice)@event.Entity).Calculator = TaxCalculator.NoTax;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Prefent NHibernate from saving default relations</title><link>http://nhforge.org/wikis/howtonh/prefent-nhibernate-from-saving-default-relations/revision/1.aspx</link><pubDate>Wed, 01 Jul 2009 13:38:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:381</guid><dc:creator>dnperfors</dc:creator><description>Revision 1 posted to How to by dnperfors on 01/07/2009 10:38:24 a.m.&lt;br /&gt;
&lt;p&gt;Let&amp;#39;s take
a look at the following code:&lt;/p&gt;
&lt;p&gt;public class TaxCalculator&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static readonly TaxCalculator
NoTax = new TaxCalculator(&amp;quot;No Tax&amp;quot;, 0.0);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public TaxCalculator()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public TaxCalculator(String
name, Double percentage)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.Name = name;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.Percentage = percentage;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Decimal CalculateTax(Decimal value)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return value * pecentage;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Guid Name { get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String Name
{ get; set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Decimal Percentage
{ get; set; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public
class Invoice &lt;br /&gt;
{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public Invoice()&lt;/p&gt;
&lt;p&gt;&amp;nbsp; : base(TaxCalculator.NoTax)&lt;/p&gt;
&lt;p&gt;{&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
&amp;nbsp;public Invoice(TaxCalculator calculator)&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.calculator
= calculator;&lt;br /&gt;
&amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;public TaxCalculator Calculator {
get; set; }&lt;br /&gt;
&amp;nbsp;public string Description
{ get; set; }&lt;br /&gt;
&amp;nbsp;public decimal Total
{ get; set; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;public decimal Tax&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; get {
return Calculator.CalculateTax(Total); }&lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;In this
example I want to be able to save a reference to the TaxCalculator, using the
following mapping:&lt;/p&gt;
&lt;pre&gt;&amp;lt;class name=&amp;quot;Invoice&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;id type=&amp;quot;guid&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;guid&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Description&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Total&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;reference name=&amp;quot;Calculator&amp;quot; /&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;class name=&amp;quot;TaxCalculator&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;id name=&amp;quot;Id&amp;quot; type=&amp;quot;guid&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;guid&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;/id&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Name&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;property name=&amp;quot;Percentage&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I assume
that TaxCalculators are created before I save the invoice (that is how
we do it at the moment). The problem we are getting into is that we can&amp;#39;t save
a reference to TaxCalculator.NoTax. This is because the NoTax object will be
created over and over again and so it will not have any Id.&lt;/p&gt;
&lt;p&gt;The
solution could be to set the Calculator property to null, but then we have to check
for a null reference. But luckily it is also possible to let NHibernate set
this property on null when persisting the object and set it to NoTax when
loading the invoice. This is done by using Events:&lt;/p&gt;
&lt;p&gt;public class FlushEvent :
DefaultFlushEntityEventListener&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void OnFlushEntity(FlushEntityEvent
@event)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (@event.Entity is Invoice &amp;amp;&amp;amp; ((Invoice)@event.Entity).Calculator
== TaxCalculator.NoTax)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Invoice)@event.Entity).Calculator = null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.OnFlushEntity(@event);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class LoadedEvent
: DefaultPostLoadEventListener, IRepositoryEvent&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public override void OnPostLoad(PostLoadEvent
@event)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;base.OnPostLoad(@event);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (@event.Entity is Invoice &amp;amp;&amp;amp; ((Invoice)@event.Entity).Calculator
== null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((Invoice)@event.Entity).Calculator = TaxCalculator.NoTax;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Contextual data using NHibernate filters</title><link>http://nhforge.org/wikis/howtonh/contextual-data-using-nhibernate-filters/revision/0.aspx</link><pubDate>Wed, 30 Mar 2011 12:41:03 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:48</guid><dc:creator>Germán Schuager</dc:creator><description>Current revision posted to How to by Germán Schuager on 30/03/2011 09:41:03 a.m.&lt;br /&gt;
&lt;h2&gt;Contextual data using NHibernate filters&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Filters&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Interceptor&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#39;m in the middle of the development
process of an application using NH for data access, and I&amp;#39;m faced
with a requirement that could be stated as follows:&lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Hilluniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Woodfielduniversity&lt;/span&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;The application needs to provide support for different
Contexts of execution, and certain entities must be context-aware,
which means that at a given time, the application only sees instances
of these entities that correspond to the current context of
execution.&lt;/i&gt;&lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Ashwooduniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Rochvilleuniversity&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Corllinsuniversity&lt;/span&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, just remember that I have several entities defined that are
used throught the entire application layer stack, so I wanted to
solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m very
proud with the solution that I came up with, and also very amazed by
the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that
I have a static class that defines the current context of execution:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public enum ContexType&lt;br /&gt;{&lt;br /&gt;	ContextA,&lt;br /&gt;	ContextB,&lt;br /&gt;}&lt;br /&gt;public static class Context&lt;br /&gt;{&lt;br /&gt;	public static ContextType Current { get; set; }&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;
Then, I create an interface that will be implemented by all the
entities that need to be contextualized:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public interface IcontextAware&lt;br /&gt;{&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;
Given a Cat class that needs to be contextualized, then I add the
property to the class and to the mapping:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class Cat : Entity, IcontextAware&lt;br /&gt;{&lt;br /&gt;	...&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;	...&lt;br /&gt;}&lt;/pre&gt;
&amp;nbsp;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;lt;class name=&amp;quot;Cat&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;	&amp;lt;property name=&amp;quot;Context&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/pre&gt;
&lt;p&gt;
The idea now, is to use the dynamic filtering capabilities of
NHibernate to only retrieve the Cats instances corresponding with the
current context every time that a query against Cat is
issued.&lt;br /&gt;Typically this means that I need to add a filter
definition to the mappings and the specify the condition for that
filter in every class mapping that need to be aware of this
behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;var filterParametersType = new Dictionary&amp;lt;string, Itype&amp;gt;(1);&lt;br /&gt;filterParametersType.Add(&amp;quot;current&amp;quot;, NhibernateUtil.Enum(typeof(ContextType)));&lt;br /&gt;cfg.AddFilterDefinition(new FilterDefinition(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;, filterParametersType));&lt;br /&gt;&lt;br /&gt;foreach (var mapping in cfg.ClassMappings)&lt;br /&gt;{&lt;br /&gt;	if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))&lt;br /&gt;	{&lt;br /&gt;		mapping.AddFilter(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;
Just do this (cfg is the NH Configuration object) before building the
session factory and it creates the correct filter definition and adds
the condition to every entity mapped that implements
IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined;
now we need to enable it in order to actually filter something. It
would be very handy if we can enable filtering at session factory
scope, but since the session factory is immutable we need to enable
it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe
something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following
interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the
context filter as soon as it is attached to the session, and&lt;br /&gt;2.
assigns the correct value to the Context property of entities
implementing IContextAware when they are persisted.&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class ContextInterceptor : EmptyInterceptor&lt;br /&gt;{&lt;br /&gt;	public override void SetSession(ISession session)&lt;br /&gt;	{&lt;br /&gt;		session.EnableFilter(&amp;quot;contextFilter&amp;quot;).SetParameter(&amp;quot;current&amp;quot;, Context.Current);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)&lt;br /&gt;	{&lt;br /&gt;		var contextAware = entity as IContextAware;&lt;br /&gt;		if (contextAware != null)&lt;br /&gt;		{&lt;br /&gt;			int index = Array.Find(propertyNames, 0, x =&amp;gt; x.Equals(&amp;quot;Context&amp;quot;));&lt;br /&gt;			state[index] = contextAware.Context = Context.Current;&lt;br /&gt;			return true;&lt;br /&gt;		}&lt;br /&gt;		return false;&lt;br /&gt;	}&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;
Every session in the application needs to be created specifying this
interceptor, but this should be an easy change (that depends on your
architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all,
the rest of the application is untouched and the requirement is
fulfilled in a very elegant way. 
&lt;/p&gt;
&lt;p&gt;(the original article is &lt;a href="http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html"&gt;here&lt;/a&gt;)&lt;/p&gt;</description></item><item><title>Contextual data using NHibernate filters</title><link>http://nhforge.org/wikis/howtonh/contextual-data-using-nhibernate-filters/revision/6.aspx</link><pubDate>Wed, 30 Mar 2011 11:35:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:385</guid><dc:creator>markweee</dc:creator><description>Revision 6 posted to How to by markweee on 30/03/2011 08:35:41 a.m.&lt;br /&gt;
&lt;h2&gt;Contextual data using NHibernate filters&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Filters, Interceptor&lt;/div&gt;

&lt;p&gt;I&amp;#39;m in the middle of the development
process of an application using NH for data access, and I&amp;#39;m faced
with a requirement that could be stated as follows: &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;Hilluniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Woodfielduniversity&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;The application needs to provide support for different
Contexts of execution, and certain entities must be context-aware,
which means that at a given time, the application only sees instances
of these entities that correspond to the current context of
execution. &lt;/i&gt;&lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;Ashwooduniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Rochvilleuniversity&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Corllinsuniversity&lt;/span&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, just remember that I have several entities defined that are
used throught the entire application layer stack, so I wanted to
solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m very
proud with the solution that I came up with, and also very amazed by
the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that
I have a static class that defines the current context of execution:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public enum ContexType&lt;br /&gt;{&lt;br /&gt;	ContextA,&lt;br /&gt;	ContextB,&lt;br /&gt;}&lt;br /&gt;public static class Context&lt;br /&gt;{&lt;br /&gt;	public static ContextType Current { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Then, I create an interface that will be implemented by all the
entities that need to be contextualized:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public interface IcontextAware&lt;br /&gt;{&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Given a Cat class that needs to be contextualized, then I add the
property to the class and to the mapping:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class Cat : Entity, IcontextAware&lt;br /&gt;{&lt;br /&gt;	...&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;	...&lt;br /&gt;}&lt;/pre&gt;
&amp;nbsp;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;lt;class name=&amp;quot;Cat&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;	&amp;lt;property name=&amp;quot;Context&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
The idea now, is to use the dynamic filtering capabilities of
NHibernate to only retrieve the Cats instances corresponding with the
current context every time that a query against Cat is
issued.&lt;br /&gt;Typically this means that I need to add a filter
definition to the mappings and the specify the condition for that
filter in every class mapping that need to be aware of this
behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;var filterParametersType = new Dictionary&amp;lt;string, Itype&amp;gt;(1);&lt;br /&gt;filterParametersType.Add(&amp;quot;current&amp;quot;, NhibernateUtil.Enum(typeof(ContextType)));&lt;br /&gt;cfg.AddFilterDefinition(new FilterDefinition(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;, filterParametersType));&lt;br /&gt;&lt;br /&gt;foreach (var mapping in cfg.ClassMappings)&lt;br /&gt;{&lt;br /&gt;	if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))&lt;br /&gt;	{&lt;br /&gt;		mapping.AddFilter(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Just do this (cfg is the NH Configuration object) before building the
session factory and it creates the correct filter definition and adds
the condition to every entity mapped that implements
IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined;
now we need to enable it in order to actually filter something. It
would be very handy if we can enable filtering at session factory
scope, but since the session factory is immutable we need to enable
it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe
something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following
interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the
context filter as soon as it is attached to the session, and&lt;br /&gt;2.
assigns the correct value to the Context property of entities
implementing IContextAware when they are persisted.&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class ContextInterceptor : EmptyInterceptor&lt;br /&gt;{&lt;br /&gt;	public override void SetSession(ISession session)&lt;br /&gt;	{&lt;br /&gt;		session.EnableFilter(&amp;quot;contextFilter&amp;quot;).SetParameter(&amp;quot;current&amp;quot;, Context.Current);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)&lt;br /&gt;	{&lt;br /&gt;		var contextAware = entity as IContextAware;&lt;br /&gt;		if (contextAware != null)&lt;br /&gt;		{&lt;br /&gt;			int index = Array.Find(propertyNames, 0, x =&amp;gt; x.Equals(&amp;quot;Context&amp;quot;));&lt;br /&gt;			state[index] = contextAware.Context = Context.Current;&lt;br /&gt;			return true;&lt;br /&gt;		}&lt;br /&gt;		return false;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Every session in the application needs to be created specifying this
interceptor, but this should be an easy change (that depends on your
architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all,
the rest of the application is untouched and the requirement is
fulfilled in a very elegant way. 
&lt;/p&gt;
&lt;p&gt;(the original article is &lt;a href="http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html"&gt;here&lt;/a&gt;)&lt;/p&gt;</description></item><item><title>Contextual data using NHibernate filters</title><link>http://nhforge.org/wikis/howtonh/contextual-data-using-nhibernate-filters/revision/5.aspx</link><pubDate>Fri, 13 Feb 2009 14:32:03 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:379</guid><dc:creator>Germán Schuager</dc:creator><description>Revision 5 posted to How to by Germán Schuager on 13/02/2009 11:32:03 a.m.&lt;br /&gt;
&lt;h2&gt;Contextual data using NHibernate filters&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;Filters&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Interceptor&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#39;m in the middle of the development
process of an application using NH for data access, and I&amp;#39;m faced
with a requirement that could be stated as follows:&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;The application needs to provide support for different
Contexts of execution, and certain entities must be context-aware,
which means that at a given time, the application only sees instances
of these entities that correspond to the current context of
execution.&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, just remember that I have several entities defined that are
used throught the entire application layer stack, so I wanted to
solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m very
proud with the solution that I came up with, and also very amazed by
the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that
I have a static class that defines the current context of execution:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public enum ContexType&lt;br /&gt;{&lt;br /&gt;	ContextA,&lt;br /&gt;	ContextB,&lt;br /&gt;}&lt;br /&gt;public static class Context&lt;br /&gt;{&lt;br /&gt;	public static ContextType Current { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Then, I create an interface that will be implemented by all the
entities that need to be contextualized:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public interface IcontextAware&lt;br /&gt;{&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Given a Cat class that needs to be contextualized, then I add the
property to the class and to the mapping:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class Cat : Entity, IcontextAware&lt;br /&gt;{&lt;br /&gt;	...&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;	...&lt;br /&gt;}&lt;/pre&gt;
&amp;nbsp;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;lt;class name=&amp;quot;Cat&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;	&amp;lt;property name=&amp;quot;Context&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
The idea now, is to use the dynamic filtering capabilities of
NHibernate to only retrieve the Cats instances corresponding with the
current context every time that a query against Cat is
issued.&lt;br /&gt;Typically this means that I need to add a filter
definition to the mappings and the specify the condition for that
filter in every class mapping that need to be aware of this
behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;var filterParametersType = new Dictionary&amp;lt;string, Itype&amp;gt;(1);&lt;br /&gt;filterParametersType.Add(&amp;quot;current&amp;quot;, NhibernateUtil.Enum(typeof(ContextType)));&lt;br /&gt;cfg.AddFilterDefinition(new FilterDefinition(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;, filterParametersType));&lt;br /&gt;&lt;br /&gt;foreach (var mapping in cfg.ClassMappings)&lt;br /&gt;{&lt;br /&gt;	if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))&lt;br /&gt;	{&lt;br /&gt;		mapping.AddFilter(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Just do this (cfg is the NH Configuration object) before building the
session factory and it creates the correct filter definition and adds
the condition to every entity mapped that implements
IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined;
now we need to enable it in order to actually filter something. It
would be very handy if we can enable filtering at session factory
scope, but since the session factory is immutable we need to enable
it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe
something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following
interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the
context filter as soon as it is attached to the session, and&lt;br /&gt;2.
assigns the correct value to the Context property of entities
implementing IContextAware when they are persisted.&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class ContextInterceptor : EmptyInterceptor&lt;br /&gt;{&lt;br /&gt;	public override void SetSession(ISession session)&lt;br /&gt;	{&lt;br /&gt;		session.EnableFilter(&amp;quot;contextFilter&amp;quot;).SetParameter(&amp;quot;current&amp;quot;, Context.Current);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)&lt;br /&gt;	{&lt;br /&gt;		var contextAware = entity as IContextAware;&lt;br /&gt;		if (contextAware != null)&lt;br /&gt;		{&lt;br /&gt;			int index = Array.Find(propertyNames, 0, x =&amp;gt; x.Equals(&amp;quot;Context&amp;quot;));&lt;br /&gt;			state[index] = contextAware.Context = Context.Current;&lt;br /&gt;			return true;&lt;br /&gt;		}&lt;br /&gt;		return false;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Every session in the application needs to be created specifying this
interceptor, but this should be an easy change (that depends on your
architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all,
the rest of the application is untouched and the requirement is
fulfilled in a very elegant way. 
&lt;/p&gt;
&lt;p&gt;(the original article is &lt;a href="http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html"&gt;here&lt;/a&gt;)&lt;/p&gt;</description></item><item><title>Contextual data using NHibernate filters</title><link>http://nhforge.org/wikis/howtonh/contextual-data-using-nhibernate-filters/revision/4.aspx</link><pubDate>Fri, 13 Feb 2009 14:32:03 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:222</guid><dc:creator>Germán Schuager</dc:creator><description>Revision 4 posted to How to by Germán Schuager on 13/02/2009 11:32:03 a.m.&lt;br /&gt;
&lt;h2&gt;Contextual data using NHibernate filters&lt;/h2&gt;
&lt;p&gt;I&amp;#39;m in the middle of the development
process of an application using NH for data access, and I&amp;#39;m faced
with a requirement that could be stated as follows:&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;The application needs to provide support for different
Contexts of execution, and certain entities must be context-aware,
which means that at a given time, the application only sees instances
of these entities that correspond to the current context of
execution.&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, just remember that I have several entities defined that are
used throught the entire application layer stack, so I wanted to
solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m very
proud with the solution that I came up with, and also very amazed by
the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that
I have a static class that defines the current context of execution:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public enum ContexType&lt;br /&gt;{&lt;br /&gt;	ContextA,&lt;br /&gt;	ContextB,&lt;br /&gt;}&lt;br /&gt;public static class Context&lt;br /&gt;{&lt;br /&gt;	public static ContextType Current { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Then, I create an interface that will be implemented by all the
entities that need to be contextualized:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public interface IcontextAware&lt;br /&gt;{&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Given a Cat class that needs to be contextualized, then I add the
property to the class and to the mapping:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class Cat : Entity, IcontextAware&lt;br /&gt;{&lt;br /&gt;	...&lt;br /&gt;	ContextType Context { get; set; }&lt;br /&gt;	...&lt;br /&gt;}&lt;/pre&gt;
&amp;nbsp;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;lt;class name=&amp;quot;Cat&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;	&amp;lt;property name=&amp;quot;Context&amp;quot;&amp;gt;&lt;br /&gt;	...&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
The idea now, is to use the dynamic filtering capabilities of
NHibernate to only retrieve the Cats instances corresponding with the
current context every time that a query against Cat is
issued.&lt;br /&gt;Typically this means that I need to add a filter
definition to the mappings and the specify the condition for that
filter in every class mapping that need to be aware of this
behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;var filterParametersType = new Dictionary&amp;lt;string, Itype&amp;gt;(1);&lt;br /&gt;filterParametersType.Add(&amp;quot;current&amp;quot;, NhibernateUtil.Enum(typeof(ContextType)));&lt;br /&gt;cfg.AddFilterDefinition(new FilterDefinition(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;, filterParametersType));&lt;br /&gt;&lt;br /&gt;foreach (var mapping in cfg.ClassMappings)&lt;br /&gt;{&lt;br /&gt;	if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))&lt;br /&gt;	{&lt;br /&gt;		mapping.AddFilter(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Just do this (cfg is the NH Configuration object) before building the
session factory and it creates the correct filter definition and adds
the condition to every entity mapped that implements
IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined;
now we need to enable it in order to actually filter something. It
would be very handy if we can enable filtering at session factory
scope, but since the session factory is immutable we need to enable
it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe
something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following
interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the
context filter as soon as it is attached to the session, and&lt;br /&gt;2.
assigns the correct value to the Context property of entities
implementing IContextAware when they are persisted.&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class ContextInterceptor : EmptyInterceptor&lt;br /&gt;{&lt;br /&gt;	public override void SetSession(ISession session)&lt;br /&gt;	{&lt;br /&gt;		session.EnableFilter(&amp;quot;contextFilter&amp;quot;).SetParameter(&amp;quot;current&amp;quot;, Context.Current);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)&lt;br /&gt;	{&lt;br /&gt;		var contextAware = entity as IContextAware;&lt;br /&gt;		if (contextAware != null)&lt;br /&gt;		{&lt;br /&gt;			&lt;span style="background: SpringGreen;"&gt;int&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;index&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Array.Find(propertyNames&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;0&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;x&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;x.Equals&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Context&amp;quot;));&lt;/span&gt;&lt;br /&gt;			&lt;span style="background: SpringGreen;"&gt;state[index&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; contextAware.Context = Context.Current;&lt;br /&gt;			return true;&lt;br /&gt;		}&lt;br /&gt;		return false;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
Every session in the application needs to be created specifying this
interceptor, but this should be an easy change (that depends on your
architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all,
the rest of the application is untouched and the requirement is
fulfilled in a very elegant way. 
&lt;/p&gt;
&lt;p&gt;(the original article is &lt;a href="http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html"&gt;here&lt;/a&gt;)&lt;/p&gt;</description></item><item><title>Contextual data using NHibernate filters</title><link>http://nhforge.org/wikis/howtonh/contextual-data-using-nhibernate-filters/revision/3.aspx</link><pubDate>Thu, 12 Feb 2009 20:25:48 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:221</guid><dc:creator>Germán Schuager</dc:creator><description>Revision 3 posted to How to by Germán Schuager on 12/02/2009 05:25:48 p.m.&lt;br /&gt;
&lt;h2&gt;Contextual data using NHibernate filters&lt;/h2&gt;
&lt;p&gt;I&amp;#39;m in the middle of the development
process of an application using NH for data access, and I&amp;#39;m faced
with a requirement that could be stated as follows:&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;The application needs to provide support for different
Contexts of execution, and certain entities must be context-aware,
which means that at a given time, the application only sees instances
of these entities that correspond to the current context of
execution.&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, just remember that I have several entities defined that are
used throught the entire application layer stack, so I wanted to
solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m very
proud with the solution that I came up with, and also very amazed by
the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that
I have a static class that defines the current context of execution:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public enum ContexType
{
	ContextA,
	ContextB,
}
public static class Context
{
	public static ContextType Current { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
Then, I create an interface that will be implemented by all the
entities that need to be contextualized:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public interface IcontextAware&lt;br /&gt;{
	ContextType Context { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
Given a Cat class that needs to be contextualized, then I add the
property to the class and to the mapping:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class Cat : Entity, IcontextAware&lt;br /&gt;{
	...
	ContextType Context { get; set; }
	...
}&lt;/pre&gt;
&amp;nbsp;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;lt;class name=&amp;quot;Cat&amp;quot;&amp;gt;
	...
	&amp;lt;property name=&amp;quot;Context&amp;quot;&amp;gt;
	...
&amp;lt;/class&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
The idea now, is to use the dynamic filtering capabilities of
NHibernate to only retrieve the Cats instances corresponding with the
current context every time that a query against Cat is
issued.&lt;br /&gt;Typically this means that I need to add a filter
definition to the mappings and the specify the condition for that
filter in every class mapping that need to be aware of this
behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;var filterParametersType = new Dictionary&amp;lt;string, Itype&amp;gt;(1);
filterParametersType.Add(&amp;quot;current&amp;quot;, NhibernateUtil.Enum(typeof(ContextType)));
cfg.AddFilterDefinition(new FilterDefinition(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;, filterParametersType));

foreach (var mapping in cfg.ClassMappings)
{
	if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))
	{
		mapping.AddFilter(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;);
	}
}
&lt;/pre&gt;
&lt;p&gt;
Just do this (cfg is the NH Configuration object) before building the
session factory and it creates the correct filter definition and adds
the condition to every entity mapped that implements
IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined;
now we need to enable it in order to actually filter something. It
would be very handy if we can enable filtering at session factory
scope, but since the session factory is immutable we need to enable
it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe
something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following
interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the
context filter as soon as it is attached to the session, and&lt;br /&gt;2.
assigns the correct value to the Context property of entities
implementing IContextAware when they are persisted.&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class ContextInterceptor : EmptyInterceptor
{
	public override void SetSession(ISession session)
	{
		session.EnableFilter(&amp;quot;contextFilter&amp;quot;).SetParameter(&amp;quot;current&amp;quot;, Context.Current);
	}

	public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
	{
		var contextAware = entity as &lt;span style="text-decoration: line-through; color: red;"&gt;IcontextAware&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;IContextAware&lt;/span&gt;;
		if (contextAware != null)
		{
			contextAware.Context = Context.Current;
			return true;
		}
		return false;
	}
}
&lt;/pre&gt;
&lt;p&gt;
Every session in the application needs to be created specifying this
interceptor, but this should be an easy change (that depends on your
architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all,
the rest of the application is untouched and the requirement is
fulfilled in a very elegant way. 
&lt;/p&gt;
&lt;p&gt;(the original article is &lt;a href="http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html"&gt;here&lt;/a&gt;)&lt;/p&gt;</description></item><item><title>Contextual data using NHibernate filters</title><link>http://nhforge.org/wikis/howtonh/contextual-data-using-nhibernate-filters/revision/2.aspx</link><pubDate>Thu, 12 Feb 2009 20:23:31 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:220</guid><dc:creator>Germán Schuager</dc:creator><description>Revision 2 posted to How to by Germán Schuager on 12/02/2009 05:23:31 p.m.&lt;br /&gt;
&lt;h2&gt;Contextual data using NHibernate filters&lt;/h2&gt;
&lt;p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;meta&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;http-equiv=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;CONTENT-TYPE&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;content=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/html;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;charset=utf-8&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;meta&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;GENERATOR&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;content=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;OpenOffice.org&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;3.0&lt;/span&gt;  &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Win32)&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;style&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--&lt;/span&gt;

&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;-&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;p&gt;I&amp;#39;m in the middle of the development
process of an application using NH for data access, and I&amp;#39;m faced
with a requirement that could be stated as follows:&lt;/p&gt;
&lt;blockquote&gt;&lt;i&gt;The application needs to provide support for different
Contexts of execution, and certain entities must be context-aware,
which means that at a given time, the application only sees instances
of these entities that correspond to the current context of
execution.&lt;/i&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, just remember that I have several entities defined that are
used throught the entire application layer stack, so I wanted to
solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m very
proud with the solution that I came up with, and also very amazed by
the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that
I have a static class that defines the current context of execution:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public enum ContexType
{
	ContextA,
	ContextB,
}
public static class Context
{
	public static ContextType Current { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
Then, I create an interface that will be implemented by all the
entities that need to be contextualized:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public interface IcontextAware&lt;br /&gt;{
	ContextType Context { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
Given a Cat class that needs to be contextualized, then I add the
property to the class and to the mapping:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class Cat : Entity, IcontextAware&lt;br /&gt;{
	...
	ContextType Context { get; set; }
	...
}

&amp;lt;class name=&amp;quot;Cat&amp;quot;&amp;gt;
	...
	&amp;lt;property name=&amp;quot;Context&amp;quot;&amp;gt;
	...
&amp;lt;/class&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
The idea now, is to use the dynamic filtering capabilities of
NHibernate to only retrieve the Cats instances corresponding with the
current context every time that a query against Cat is
issued.&lt;br /&gt;Typically this means that I need to add a filter
definition to the mappings and the specify the condition for that
filter in every class mapping that need to be aware of this
behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;var filterParametersType = new Dictionary&amp;lt;string, Itype&amp;gt;(1);
filterParametersType.Add(&amp;quot;current&amp;quot;, NhibernateUtil.Enum(typeof(ContextType)));
cfg.AddFilterDefinition(new FilterDefinition(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;, filterParametersType));

foreach (var mapping in cfg.ClassMappings)
{
	if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))
	{
		mapping.AddFilter(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;);
	}
}
&lt;/pre&gt;
&lt;p&gt;
Just do this (cfg is the NH Configuration object) before building the
session factory and it creates the correct filter definition and adds
the condition to every entity mapped that implements
IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined;
now we need to enable it in order to actually filter something. It
would be very handy if we can enable filtering at session factory
scope, but since the session factory is immutable we need to enable
it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe
something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following
interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the
context filter as soon as it is attached to the session, and&lt;br /&gt;2.
assigns the correct value to the Context property of entities
implementing IContextAware when they are persisted.&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class ContextInterceptor : EmptyInterceptor
{
	public override void SetSession(ISession session)
	{
		session.EnableFilter(&amp;quot;contextFilter&amp;quot;).SetParameter(&amp;quot;current&amp;quot;, Context.Current);
	}

	public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
	{
		var contextAware = entity as IcontextAware;
		if (contextAware != null)
		{
			contextAware.Context = Context.Current;
			return true;
		}
		return false;
	}
}
&lt;/pre&gt;
&lt;p&gt;
Every session in the application needs to be created specifying this
interceptor, but this should be an easy change (that depends on your
architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all,
the rest of the application is untouched and the requirement is
fulfilled in a very elegant way. 
&lt;/p&gt;
&lt;p&gt;(the original article is &lt;a href="http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html"&gt;here&lt;/a&gt;)&lt;/p&gt;</description></item><item><title>Contextual data using NHibernate filters</title><link>http://nhforge.org/wikis/howtonh/contextual-data-using-nhibernate-filters/revision/1.aspx</link><pubDate>Thu, 12 Feb 2009 20:22:50 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:219</guid><dc:creator>Germán Schuager</dc:creator><description>Revision 1 posted to How to by Germán Schuager on 12/02/2009 05:22:50 p.m.&lt;br /&gt;
&lt;p&gt;
&amp;lt;meta http-equiv=&amp;quot;CONTENT-TYPE&amp;quot; content=&amp;quot;text/html; charset=utf-8&amp;quot; /&amp;gt;
&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;
&amp;lt;meta name=&amp;quot;GENERATOR&amp;quot; content=&amp;quot;OpenOffice.org 3.0  (Win32)&amp;quot; /&amp;gt;
&amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;&amp;lt;!--

--&amp;gt;&amp;lt;/style&amp;gt;
&lt;/p&gt;
&lt;p&gt;I&amp;#39;m in the middle of the development
process of an application using NH for data access, and I&amp;#39;m faced
with a requirement that could be stated as follows:&lt;/p&gt;
&lt;blockquote&gt;The application needs to provide support for different
Contexts of execution, and certain entities must be context-aware,
which means that at a given time, the application only sees instances
of these entities that correspond to the current context of
execution.&lt;/blockquote&gt;
&lt;p&gt;Now, just remember that I have several entities defined that are
used throught the entire application layer stack, so I wanted to
solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m very
proud with the solution that I came up with, and also very amazed by
the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that
I have a static class that defines the current context of execution:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public enum ContexType
{
	ContextA,
	ContextB,
}
public static class Context
{
	public static ContextType Current { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
Then, I create an interface that will be implemented by all the
entities that need to be contextualized:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public interface IcontextAware&lt;br /&gt;{
	ContextType Context { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
Given a Cat class that needs to be contextualized, then I add the
property to the class and to the mapping:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class Cat : Entity, IcontextAware&lt;br /&gt;{
	...
	ContextType Context { get; set; }
	...
}

&amp;lt;class name=&amp;quot;Cat&amp;quot;&amp;gt;
	...
	&amp;lt;property name=&amp;quot;Context&amp;quot;&amp;gt;
	...
&amp;lt;/class&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
The idea now, is to use the dynamic filtering capabilities of
NHibernate to only retrieve the Cats instances corresponding with the
current context every time that a query against Cat is
issued.&lt;br /&gt;Typically this means that I need to add a filter
definition to the mappings and the specify the condition for that
filter in every class mapping that need to be aware of this
behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;var filterParametersType = new Dictionary&amp;lt;string, Itype&amp;gt;(1);
filterParametersType.Add(&amp;quot;current&amp;quot;, NhibernateUtil.Enum(typeof(ContextType)));
cfg.AddFilterDefinition(new FilterDefinition(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;, filterParametersType));

foreach (var mapping in cfg.ClassMappings)
{
	if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))
	{
		mapping.AddFilter(&amp;quot;contextFilter&amp;quot;, &amp;quot;:current = Context&amp;quot;);
	}
}
&lt;/pre&gt;
&lt;p&gt;
Just do this (cfg is the NH Configuration object) before building the
session factory and it creates the correct filter definition and adds
the condition to every entity mapped that implements
IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined;
now we need to enable it in order to actually filter something. It
would be very handy if we can enable filtering at session factory
scope, but since the session factory is immutable we need to enable
it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe
something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following
interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the
context filter as soon as it is attached to the session, and&lt;br /&gt;2.
assigns the correct value to the Context property of entities
implementing IContextAware when they are persisted.&lt;/p&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;public class ContextInterceptor : EmptyInterceptor
{
	public override void SetSession(ISession session)
	{
		session.EnableFilter(&amp;quot;contextFilter&amp;quot;).SetParameter(&amp;quot;current&amp;quot;, Context.Current);
	}

	public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
	{
		var contextAware = entity as IcontextAware;
		if (contextAware != null)
		{
			contextAware.Context = Context.Current;
			return true;
		}
		return false;
	}
}
&lt;/pre&gt;
&lt;p&gt;
Every session in the application needs to be created specifying this
interceptor, but this should be an easy change (that depends on your
architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all,
the rest of the application is untouched and the requirement is
fulfilled in a very elegant way. 
&lt;/p&gt;
&lt;p&gt;(the original article is &lt;a href="http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html"&gt;here&lt;/a&gt;)&lt;/p&gt;</description></item><item><title>Mapping a view and Schema Export Workaround</title><link>http://nhforge.org/wikis/howtonh/mapping-a-view-and-schema-export-workaround/revision/0.aspx</link><pubDate>Wed, 30 Mar 2011 11:43:08 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:10</guid><dc:creator>markweee</dc:creator><description>Current revision posted to How to by markweee on 30/03/2011 08:43:08 a.m.&lt;br /&gt;
&lt;h2&gt;Mapping a view and Schema Export Workaround&lt;/h2&gt;
&lt;p&gt;NHibernate can easily map a View as any other Table.&lt;/p&gt;
&lt;p&gt;This works as a charm, but it has a problem with Schema Export. Schema Export will detect the view as a table and will attempt to create the unexisting table.&lt;/p&gt;
&lt;p&gt;A workaround for this is using &amp;lt;database-object&amp;gt; tag to Drop the unwillable table on creation, someting like:&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:88f9b13c-1b26-4e08-a789-94e8cdc0b668" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;background-color:#dddddd;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;database-object&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;create&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;DROP TABLE UnWantedTable&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;create&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;drop &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;database-object&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;An idea to avoid this workaround is having some hint to SchemaExport to indicate that this class should not enter in the Export ... &lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;About&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Ashwood&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;About&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Rochville&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;About&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Adison&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;High&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;School&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I opened this suggestion in this JIRA, you can vote for it if you also map your views and do not want the wrong creation of a table on export! &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;About&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;Corllins&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;About&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Hill&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.onlineeducationfacts.com/online-education-facts/woodfield-university.htm"&gt;
&lt;span style="background: SpringGreen;"&gt;Woodfield&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://jira.nhibernate.org/browse/NH-1274"&gt;http://jira.nhibernate.org/browse/NH-1274&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New Feature&lt;/b&gt;:&amp;nbsp;&lt;a href="/blogs/nhibernate/archive/2008/12/15/customizing-schema-export-with-schema-action.aspx"&gt;Customizing Schema Export with schema-action&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Mapping a view and Schema Export Workaround</title><link>http://nhforge.org/wikis/howtonh/mapping-a-view-and-schema-export-workaround/revision/6.aspx</link><pubDate>Tue, 16 Dec 2008 19:08:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:382</guid><dc:creator>Dario Quintana</dc:creator><description>Revision 6 posted to How to by Dario Quintana on 16/12/2008 04:08:56 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping a view and Schema Export Workaround&lt;/h2&gt;
&lt;p&gt;NHibernate can easily map a View as any other Table.&lt;/p&gt;
&lt;p&gt;This works as a charm, but it has a problem with Schema Export. Schema Export will detect the view as a table and will attempt to create the unexisting table.&lt;/p&gt;
&lt;p&gt;A workaround for this is using &amp;lt;database-object&amp;gt; tag to Drop the unwillable table on creation, someting like:&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:88f9b13c-1b26-4e08-a789-94e8cdc0b668" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;background-color:#dddddd;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;database-object&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;create&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;DROP TABLE UnWantedTable&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;create&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;drop &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;database-object&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;An idea to avoid this workaround is having some hint to SchemaExport to indicate that this class should not enter in the Export ...&lt;/p&gt;
&lt;p&gt;I opened this suggestion in this JIRA, you can vote for it if you also map your views and do not want the wrong creation of a table on export!&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://jira.nhibernate.org/browse/NH-1274"&gt;http://jira.nhibernate.org/browse/NH-1274&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;New&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Feature&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&amp;nbsp;&lt;a href="/blogs/nhibernate/archive/2008/12/15/customizing-schema-export-with-schema-action.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Customizing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Schema&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Export&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;schema-action&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Mapping a view and Schema Export Workaround</title><link>http://nhforge.org/wikis/howtonh/mapping-a-view-and-schema-export-workaround/revision/5.aspx</link><pubDate>Fri, 05 Sep 2008 20:08:09 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:204</guid><dc:creator>Gustavo</dc:creator><description>Revision 5 posted to How to by Gustavo on 05/09/2008 05:08:09 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping a view and Schema Export Workaround&lt;/h2&gt;
&lt;p&gt;NHibernate can easily map a View as any other Table.&lt;/p&gt;
&lt;p&gt;This works as a charm, but it has a problem with Schema Export. Schema Export will detect the view as a table and will attempt to create the unexisting table.&lt;/p&gt;
&lt;p&gt;A workaround for this is using &amp;lt;database-object&amp;gt; tag to Drop the unwillable table on creation, someting like:&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:88f9b13c-1b26-4e08-a789-94e8cdc0b668" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;background-color:#dddddd;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;database-object&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;create&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;DROP TABLE UnWantedTable&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;create&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;drop &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;database-object&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;An idea to avoid this workaround is having some hint to SchemaExport to indicate that this class should not enter in the Export ...&lt;/p&gt;
&lt;p&gt;I opened this suggestion in this JIRA, you can vote for it if you also &lt;span style="text-decoration: line-through; color: red;"&gt;want&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; map your views &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;want&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;wrong&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;export&lt;/span&gt;!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jira.nhibernate.org/browse/NH-1274" target="_blank"&gt;http://jira.nhibernate.org/browse/NH-1274&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Mapping a view and Schema Export Workaround</title><link>http://nhforge.org/wikis/howtonh/mapping-a-view-and-schema-export-workaround/revision/4.aspx</link><pubDate>Fri, 05 Sep 2008 20:00:42 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:25</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 4 posted to How to by Fabio Maulo on 05/09/2008 05:00:42 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping a view and Schema Export Workaround&lt;/h2&gt;
&lt;p&gt;NHibernate can easily map a View as any other Table.&lt;/p&gt;
&lt;p&gt;This works as a charm, but it has a problem with Schema Export. Schema Export will detect the view as a table and will attempt to create the unexisting table.&lt;/p&gt;
&lt;p&gt;A workaround for this is using &amp;lt;database-object&amp;gt; tag to Drop the unwillable table on creation, someting like:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-weight:normal;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;language=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;XML&amp;quot;]&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;database-object&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;create&amp;gt;DROP&lt;/span&gt; &lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:88f9b13c-1b26-4e08-a789-94e8cdc0b668" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;background-color:#dddddd;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;database-object&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;span style="background: SpringGreen;"&gt;DROP&lt;/span&gt; TABLE UnWantedTable&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/create&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;drop&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;drop&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/database-object&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/code]&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;database-object&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;An idea to avoid this workaround is having some hint to SchemaExport to indicate that this class should not enter in the Export ...&lt;/p&gt;
&lt;p&gt;I opened this suggestion in this JIRA, you can vote for it if you also want to map your views!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jira.nhibernate.org/browse/NH-1274" target="_blank"&gt;http://jira.nhibernate.org/browse/NH-1274&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Mapping a view and Schema Export Workaround</title><link>http://nhforge.org/wikis/howtonh/mapping-a-view-and-schema-export-workaround/revision/3.aspx</link><pubDate>Fri, 05 Sep 2008 19:54:03 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:24</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 3 posted to How to by Fabio Maulo on 05/09/2008 04:54:03 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping a view and Schema Export Workaround&lt;/h2&gt;
&lt;p&gt;NHibernate can easily map a View as any other Table.&lt;/p&gt;
&lt;p&gt;This works as a charm, but it has a problem with Schema Export. Schema Export will detect the view as a table and will attempt to create the unexisting table.&lt;/p&gt;
&lt;p&gt;A workaround for this is using &amp;lt;database-object&amp;gt; tag to Drop the unwillable table on creation, someting like:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-weight:normal;"&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;language=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;XML&amp;quot;]&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;database-object&amp;gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;lt;create&amp;gt;DROP TABLE UnWantedTable&amp;lt;/create&amp;gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&amp;lt;drop /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/database-object&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/code]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;An idea to avoid this workaround is having some hint to SchemaExport to indicate that this class should not enter in the Export ...&lt;/p&gt;
&lt;p&gt;I opened this suggestion in this JIRA, you can vote for it if you also want to map your views!&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://jira.nhibernate.org/browse/NH-1274"&gt;http://jira.nhibernate.org/browse/NH-1274&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Mapping a view and Schema Export Workaround</title><link>http://nhforge.org/wikis/howtonh/mapping-a-view-and-schema-export-workaround/revision/2.aspx</link><pubDate>Fri, 05 Sep 2008 19:44:48 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:23</guid><dc:creator>Gustavo</dc:creator><description>Revision 2 posted to How to by Gustavo on 05/09/2008 04:44:48 p.m.&lt;br /&gt;
&lt;h2&gt;Mapping a view and Schema Export Workaround&lt;/h2&gt;
&lt;p&gt;NHibernate can easily map a View as any other Table.&lt;/p&gt;
&lt;p&gt;This works as a charm, but it has a problem with Schema Export. Schema Export will detect the view as a table and will attempt to create the unexisting table.&lt;/p&gt;
&lt;p&gt;A workaround for this is using &amp;lt;database-object&amp;gt; tag to Drop the unwillable table on creation, someting like:&lt;/p&gt;
&lt;p&gt;&amp;lt;database-object&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;create&amp;gt;DROP TABLE UnWantedTable&amp;lt;/create&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;drop /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/database-object&amp;gt;&lt;/p&gt;
&lt;p&gt;An idea to avoid this workaround is having some hint to SchemaExport to indicate that this class should not enter in the Export ...&lt;/p&gt;
&lt;p&gt;I opened this suggestion in this JIRA, you can vote for it if you also want to map your views!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jira.nhibernate.org/browse/NH-1274" target="_blank"&gt;http://jira.nhibernate.org/browse/NH-1274&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Mapping a view and Schema Export Workaround</title><link>http://nhforge.org/wikis/howtonh/mapping-a-view-and-schema-export-workaround/revision/1.aspx</link><pubDate>Fri, 05 Sep 2008 19:37:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:22</guid><dc:creator>Gustavo</dc:creator><description>Revision 1 posted to How to by Gustavo on 05/09/2008 04:37:53 p.m.&lt;br /&gt;
&lt;p&gt;NHibernate can easily map a View as any other Table.&lt;/p&gt;
&lt;p&gt;This works as a charm, but it has a problem with Schema Export. Schema Export will detect the view as a table and will attempt to create the unexisting table.&lt;/p&gt;
&lt;p&gt;A workaround for this is using &amp;lt;database-object&amp;gt; tag to Drop the unwillable table on creation, someting like:&lt;/p&gt;
&lt;p&gt;&amp;lt;database-object&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;create&amp;gt;DROP TABLE UnWantedTable&amp;lt;/create&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;drop /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/database-object&amp;gt;&lt;/p&gt;
&lt;p&gt;An idea to avoid this workaround is having some hint to SchemaExport to indicate that this class should not enter in the Export ...&lt;/p&gt;
&lt;p&gt;I opened this suggestion in this JIRA, you can vote for it if you also want to map your views!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jira.nhibernate.org/browse/NH-1274"&gt;http://jira.nhibernate.org/browse/NH-1274&lt;/a&gt;&lt;/p&gt;</description></item><item><title>The best way to get NHibernate community support ? Submit precise description of the problem</title><link>http://nhforge.org/wikis/howtonh/the-best-way-to-get-nhibernate-community-support-submit-precise-description-of-the-problem/revision/0.aspx</link><pubDate>Wed, 30 Mar 2011 11:37:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:62</guid><dc:creator>markweee</dc:creator><description>Current revision posted to How to by markweee on 30/03/2011 08:37:15 a.m.&lt;br /&gt;
&lt;h2&gt;The best way to get NHibernate community support ? Submit precise description of the problem&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;support&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bug&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;jira&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;NHibernate has fast growing community with NHibernate Users mailing list as a place&lt;br /&gt;where you can get support with your NHibernate issues from wide range of people&lt;br /&gt;including NHibernate core developers. &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;Hill&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Review&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Woodfield&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Review&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;However, nobody is powerful enough without sufficient description of your problem.&lt;br /&gt;To get the best possible answer for your problem as fast as possible,&amp;nbsp; &lt;a href="http://www.reviewonlineuniversity.com/universities/ashwood-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Ashwood&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Review&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Rochville&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Review&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Adison&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;High&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;School&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Review&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;keep following recommendations in mind before you post to NH-Users mailing list:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;1. Post relevant mappings&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can get idea of how your domain look like&lt;br /&gt;&lt;br /&gt;2. Post relevant code for the domain classes&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how your code look like&lt;br /&gt;&lt;br /&gt;3. Post relevant code that cause an issue&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see WHAT are you doing&lt;br /&gt;&lt;br /&gt;4. When receiving exception, post exception including stack trace and same for inner exception(s) if they exist&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see what is the problem&lt;br /&gt;&lt;br /&gt;5.Where there is no exception, write what&amp;#39;s the actual result versus what are you expecting&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how problem can be fixed&lt;br /&gt;&lt;br /&gt;Remember that people are glad to help you with your problem if they know WHAT is your problem.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Before posting issue to Issue tracker&lt;/b&gt; (JIRA), please consider following points:&lt;/p&gt;
&lt;p&gt;1. JIRA is NOT support forum. Before posting your problem directly to JIRA, please post it to support mailing list first.&lt;br /&gt;There is a good chance you get your issue resolved in mailing list or get a confirmation that your problem really belongs to JIRA. Also do not create new JIRA before you get answer from mailing list.&lt;/p&gt;
&lt;p&gt;2. If you are new to JIRA please read and keep in mind short guidance of what JIRA fields mean:&lt;a href="http://nhjira.koah.net/secure/ShowConstantsHelp.jspa"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://nhjira.koah.net/secure/ShowConstantsHelp.jspa"&gt;http://nhjira.koah.net/secure/ShowConstantsHelp.jspa&lt;/a&gt;&lt;br /&gt;Correct qualification of issue is important for both developers and users.&lt;br /&gt;&lt;br /&gt;3. Keep in mind the most important part of new issue in JIRA is Description.&lt;br /&gt;Description is available during process of creating new issue and for issues (even with attached test case) it&amp;#39;s the most&amp;nbsp;&amp;nbsp;&amp;nbsp; important part at all.&lt;br /&gt;Do not use just links in Description, when linked server goes down description is lost in time and developers are not&amp;nbsp; able to recover it&amp;nbsp; even they would like to do that.&lt;br /&gt;Here is an example how NOT to create JIRA issue:&lt;br /&gt;&lt;a href="http://nhjira.koah.net/browse/NH-1808"&gt;http://nhjira.koah.net/browse/NH-1808&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;4. If you are attaching testcase for the issue, there is a great &lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.aspx"&gt;document&lt;/a&gt; explaining how to do it properly.&lt;/p&gt;</description></item><item><title>The best way to get NHibernate community support ? Submit precise description of the problem</title><link>http://nhforge.org/wikis/howtonh/the-best-way-to-get-nhibernate-community-support-submit-precise-description-of-the-problem/revision/6.aspx</link><pubDate>Tue, 02 Jun 2009 18:33:55 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:380</guid><dc:creator>jsevcik</dc:creator><description>Revision 6 posted to How to by jsevcik on 02/06/2009 03:33:55 p.m.&lt;br /&gt;
&lt;h2&gt;The best way to get NHibernate community support ? Submit precise description of the problem&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: support bug jira&lt;/div&gt;

&lt;p&gt;NHibernate has fast growing community with NHibernate Users mailing list as a place&lt;br /&gt;where you can get support with your NHibernate issues from wide range of people&lt;br /&gt;including NHibernate core developers.&lt;br /&gt;&lt;br /&gt;However, nobody is powerful enough without sufficient description of your problem.&lt;br /&gt;To get the best possible answer for your problem as fast as possible, &lt;br /&gt;&lt;b&gt;keep following recommendations in mind before you post to NH-Users mailing list:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;1. Post relevant mappings&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can get idea of how your domain look like&lt;br /&gt;&lt;br /&gt;2. Post relevant code for the domain classes&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how your code look like&lt;br /&gt;&lt;br /&gt;3. Post relevant code that cause an issue&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see WHAT are you doing&lt;br /&gt;&lt;br /&gt;4. When receiving exception, post exception including stack trace and same for inner exception(s) if they exist&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see what is the problem&lt;br /&gt;&lt;br /&gt;5.Where there is no exception, write what&amp;#39;s the actual result versus what are you expecting&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how problem can be fixed&lt;br /&gt;&lt;br /&gt;Remember that people are glad to help you with your problem if they know WHAT is your problem.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Before posting issue to Issue tracker&lt;/b&gt; (JIRA), please consider following points:&lt;/p&gt;
&lt;p&gt;1. JIRA is NOT support forum. Before posting your problem directly to JIRA, please post it to support mailing list first.&lt;br /&gt;There is a good chance you get your issue resolved in mailing list or get a confirmation that your problem really belongs to JIRA. Also do not create new JIRA before you get answer from mailing list.&lt;/p&gt;
&lt;p&gt;2. If you are new to JIRA please read and keep in mind short guidance of what JIRA fields mean:&lt;a href="http://nhjira.koah.net/secure/ShowConstantsHelp.jspa"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://nhjira.koah.net/secure/ShowConstantsHelp.jspa"&gt;http://nhjira.koah.net/secure/ShowConstantsHelp.jspa&lt;/a&gt;&lt;br /&gt;Correct qualification of issue is important for both developers and users.&lt;br /&gt;&lt;br /&gt;3. Keep in mind the most important part of new issue in JIRA is Description.&lt;br /&gt;Description is available during process of creating new issue and for issues (even with attached test case) it&amp;#39;s the most&amp;nbsp;&amp;nbsp;&amp;nbsp; important part at all.&lt;br /&gt;Do not use just links in Description, when linked server goes down description is lost in time and developers are not&lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt;&amp;nbsp; able to recover it&amp;nbsp; even they would like to do that.&lt;br /&gt;Here is an example how NOT to create JIRA issue:&lt;br /&gt;&lt;a href="http://nhjira.koah.net/browse/NH-1808"&gt;http://nhjira.koah.net/browse/NH-1808&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;4. If you are attaching testcase for the issue, there is a great &lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.aspx"&gt;document&lt;/a&gt; explaining how to do it properly.&lt;/p&gt;</description></item><item><title>The best way to get NHibernate community support ? Submit precise description of the problem</title><link>http://nhforge.org/wikis/howtonh/the-best-way-to-get-nhibernate-community-support-submit-precise-description-of-the-problem/revision/5.aspx</link><pubDate>Tue, 02 Jun 2009 16:19:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:278</guid><dc:creator>jsevcik</dc:creator><description>Revision 5 posted to How to by jsevcik on 02/06/2009 01:19:24 p.m.&lt;br /&gt;
&lt;h2&gt;The best way to get NHibernate community support ? Submit precise description of the problem&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: support bug &lt;span style="background: SpringGreen;"&gt;jira&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;NHibernate has fast growing community with NHibernate Users mailing list as a place&lt;br /&gt;where you can get support with your NHibernate issues from wide range of people&lt;br /&gt;including NHibernate core developers.&lt;br /&gt;&lt;br /&gt;However, nobody is powerful enough without sufficient description of your problem.&lt;br /&gt;To get the best possible answer for your problem as fast as possible, &lt;br /&gt;&lt;b&gt;keep following recommendations in mind before you &lt;span style="text-decoration: line-through; color: red;"&gt;click&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Send&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mail&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;client&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NH-Users&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mailing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt;:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;1. Post relevant mappings&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can get idea of how your domain look like&lt;br /&gt;&lt;br /&gt;2. Post relevant code for the domain classes&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how your code look like&lt;br /&gt;&lt;br /&gt;3. Post relevant code that cause an issue&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see WHAT are you doing&lt;br /&gt;&lt;br /&gt;4. When receiving exception, post exception including stack trace and same for inner exception(s) if they exist&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see what is the problem&lt;br /&gt;&lt;br /&gt;5.Where there is no exception, write what&amp;#39;s the actual result versus what are you expecting&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how problem can be fixed&lt;br /&gt;&lt;br /&gt;Remember that people are glad to help you with your problem if they know WHAT is your problem.&lt;br /&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Note&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;For&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Before&lt;/span&gt; posting &lt;span style="text-decoration: line-through; color: red;"&gt;testcases&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt; to &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; Issue tracker&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;JIRA),&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;please&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;consider&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;points&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;1&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NOT&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;support&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;forum&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;posting&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;directly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;please&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;support&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mailing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;good&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;chance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;resolved&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mailing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;confirmation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;really&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;belongs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Also&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;answer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mailing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;2&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;please&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;read&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;keep&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mind&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;short&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;guidance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fields&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mean&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;a href="http://nhjira.koah.net/secure/ShowConstantsHelp.jspa"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://nhjira.koah.net/secure/ShowConstantsHelp.jspa"&gt;&lt;span style="background: SpringGreen;"&gt;http://nhjira.koah.net/secure/ShowConstantsHelp.jspa&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;Correct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;qualification&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;important&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;both&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;developers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;users&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;3&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Keep&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mind&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;most&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;important&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;part&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Description&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;Description&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;available&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;during&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;process&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issues&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;even&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;attached&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;test&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;case&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;most&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;important&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;part&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;Do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;links&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Description&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;linked&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;server&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;goes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;down&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;description&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lost&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;developers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;able&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recover&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;even&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;would&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;like&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;Here&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NOT&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;create&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;JIRA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;a href="http://nhjira.koah.net/browse/NH-1808"&gt;&lt;span style="background: SpringGreen;"&gt;http://nhjira.koah.net/browse/NH-1808&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;4&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;attaching&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;testcase&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; there is a &lt;span style="background: SpringGreen;"&gt;great&lt;/span&gt; &lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.aspx"&gt;document&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;describing&lt;/span&gt; &lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;explaining&lt;/span&gt; how to do it properly.&lt;/p&gt;</description></item><item><title>The best way to get NHibernate community support ? Submit precise description of the problem</title><link>http://nhforge.org/wikis/howtonh/the-best-way-to-get-nhibernate-community-support-submit-precise-description-of-the-problem/revision/4.aspx</link><pubDate>Mon, 01 Jun 2009 19:03:13 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:276</guid><dc:creator>jsevcik</dc:creator><description>Revision 4 posted to How to by jsevcik on 01/06/2009 04:03:13 p.m.&lt;br /&gt;
&lt;h2&gt;The best way to get NHibernate community support ? Submit precise description of &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; problem&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: support bug&lt;/div&gt;

&lt;p&gt;NHibernate has fast growing community with NHibernate Users mailing list as a place&lt;br /&gt;where you can get support with your NHibernate issues from wide range of people&lt;br /&gt;including NHibernate core developers.&lt;br /&gt;&lt;br /&gt;However, nobody is powerful enough without sufficient description of your problem.&lt;br /&gt;To get the best possible answer for your problem as fast as possible, &lt;br /&gt;keep following recommendations in mind before you click on &amp;#39;Send&amp;#39; in your mail client:&lt;br /&gt;&lt;br /&gt;1. Post relevant mappings&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can get idea of how your domain look like&lt;br /&gt;&lt;br /&gt;2. Post relevant code for the domain classes&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how your code look like&lt;br /&gt;&lt;br /&gt;3. Post relevant code that cause an issue&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see WHAT are you doing&lt;br /&gt;&lt;br /&gt;4. When receiving exception, post exception including stack trace and same for inner exception(s) if they exist&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see what is the problem&lt;br /&gt;&lt;br /&gt;5.Where there is no exception, write what&amp;#39;s the actual result versus what are you expecting&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how problem can be fixed&lt;br /&gt;&lt;br /&gt;Remember that people are glad to help you with your problem if they know WHAT is your problem.&lt;br /&gt;&lt;br /&gt;Note: For posting testcases to NHibernate Issue tracker&lt;br /&gt;there is a&amp;nbsp;&lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.aspx"&gt;document&lt;/a&gt; describing how to do it properly.&lt;/p&gt;</description></item><item><title>The best way to get NHibernate community support ? Submit precise description of problem</title><link>http://nhforge.org/wikis/howtonh/the-best-way-to-get-nhibernate-community-support-submit-precise-description-of-the-problem/revision/3.aspx</link><pubDate>Mon, 01 Jun 2009 19:02:36 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:275</guid><dc:creator>jsevcik</dc:creator><description>Revision 3 posted to How to by jsevcik on 01/06/2009 04:02:36 p.m.&lt;br /&gt;
&lt;h2&gt;The best way to get NHibernate community support ? Submit precise description of problem&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: support bug&lt;/div&gt;

&lt;p&gt;NHibernate has fast growing community with NHibernate Users mailing list as a place&lt;br /&gt;where you can get support with your NHibernate issues from wide range of people&lt;br /&gt;including NHibernate core developers.&lt;br /&gt;&lt;br /&gt;However, nobody is powerful enough without sufficient description of your problem.&lt;br /&gt;To get the best possible answer for your problem as fast as possible, &lt;br /&gt;keep following recommendations in mind before you click on &amp;#39;Send&amp;#39; in your mail client:&lt;br /&gt;&lt;br /&gt;1. Post relevant mappings&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can get idea of how your domain look like&lt;br /&gt;&lt;br /&gt;2. Post relevant code for the domain classes&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how your code look like&lt;br /&gt;&lt;br /&gt;3. Post relevant code that cause an issue&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see WHAT are you doing&lt;br /&gt;&lt;br /&gt;4. When receiving exception, post exception including stack trace and same for inner exception(s) if they exist&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see what is the problem&lt;br /&gt;&lt;br /&gt;5.Where there is no exception, write what&amp;#39;s the actual result versus what are you expecting&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how problem can be fixed&lt;br /&gt;&lt;br /&gt;Remember that people are glad to help you with your problem if they know WHAT is your problem.&lt;br /&gt;&lt;br /&gt;Note: For posting testcases to NHibernate Issue tracker&lt;br /&gt;there is a&lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.asp%20%20x"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;howt[[howtonh:/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.aspx|howto]]o&lt;/span&gt;&amp;nbsp;&lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;document&lt;/span&gt;&lt;/a&gt; describing how to do it properly.&lt;/p&gt;</description></item><item><title>The best way to get NHibernate community support ? Submit precise description of problem</title><link>http://nhforge.org/wikis/howtonh/the-best-way-to-get-nhibernate-community-support-submit-precise-description-of-the-problem/revision/2.aspx</link><pubDate>Mon, 01 Jun 2009 19:01:48 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:274</guid><dc:creator>jsevcik</dc:creator><description>Revision 2 posted to How to by jsevcik on 01/06/2009 04:01:48 p.m.&lt;br /&gt;
&lt;h2&gt;The best way to get NHibernate community support ? Submit precise description of problem&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: support bug&lt;/div&gt;

&lt;p&gt;NHibernate has fast growing community with NHibernate Users mailing list as a place&lt;br /&gt;where you can get support with your NHibernate issues from wide range of people&lt;br /&gt;including NHibernate core developers.&lt;br /&gt;&lt;br /&gt;However, nobody is powerful enough without sufficient description of your problem.&lt;br /&gt;To get the best possible answer for your problem as fast as possible, &lt;br /&gt;keep following recommendations in mind before you click on &amp;#39;Send&amp;#39; in your mail client:&lt;br /&gt;&lt;br /&gt;1. Post relevant mappings&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can get idea of how your domain look like&lt;br /&gt;&lt;br /&gt;2. Post relevant code for the domain classes&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how your code look like&lt;br /&gt;&lt;br /&gt;3. Post relevant code that cause an issue&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see WHAT are you doing&lt;br /&gt;&lt;br /&gt;4. When receiving exception, post exception including stack trace and same for inner exception(s) if they exist&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see what is the problem&lt;br /&gt;&lt;br /&gt;5.Where there is no exception, write what&amp;#39;s the actual result versus what are you expecting&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how problem can be fixed&lt;br /&gt;&lt;br /&gt;Remember that people are glad to help you with your problem if they know WHAT is your problem.&lt;br /&gt;&lt;br /&gt;Note: For posting testcases to NHibernate Issue tracker&lt;br /&gt;there is a &lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.asp%20%20x"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;howto&lt;/span&gt;&lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.asp%20%20x"&gt;&lt;span style="background: SpringGreen;"&gt;howt[[howtonh:/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.aspx|howto]]o&lt;/span&gt;&lt;/a&gt; describing how to do it properly.&lt;/p&gt;</description></item><item><title>The best way to get NHibernate community support ? Submit precise description of problem</title><link>http://nhforge.org/wikis/howtonh/the-best-way-to-get-nhibernate-community-support-submit-precise-description-of-the-problem/revision/1.aspx</link><pubDate>Mon, 01 Jun 2009 19:01:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:273</guid><dc:creator>jsevcik</dc:creator><description>Revision 1 posted to How to by jsevcik on 01/06/2009 04:01:02 p.m.&lt;br /&gt;
&lt;p&gt;NHibernate has fast growing community with NHibernate Users mailing list as a place&lt;br /&gt;where you can get support with your NHibernate issues from wide range of people&lt;br /&gt;including NHibernate core developers.&lt;br /&gt;&lt;br /&gt;However, nobody is powerful enough without sufficient description of your problem.&lt;br /&gt;To get the best possible answer for your problem as fast as possible, &lt;br /&gt;keep following recommendations in mind before you click on &amp;#39;Send&amp;#39; in your mail client:&lt;br /&gt;&lt;br /&gt;1. Post relevant mappings&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can get idea of how your domain look like&lt;br /&gt;&lt;br /&gt;2. Post relevant code for the domain classes&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how your code look like&lt;br /&gt;&lt;br /&gt;3. Post relevant code that cause an issue&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see WHAT are you doing&lt;br /&gt;&lt;br /&gt;4. When receiving exception, post exception including stack trace and same for inner exception(s) if they exist&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see what is the problem&lt;br /&gt;&lt;br /&gt;5.Where there is no exception, write what&amp;#39;s the actual result versus what are you expecting&lt;br /&gt;&amp;nbsp;&amp;nbsp; ...so people can see how problem can be fixed&lt;br /&gt;&lt;br /&gt;Remember that people are glad to help you with your problem if they know WHAT is your problem.&lt;br /&gt;&lt;br /&gt;Note: For posting testcases to NHibernate Issue tracker&lt;br /&gt;there is a &lt;a href="/blogs/nhibernate/archive/2008/10/04/the-best-way-to-solve-nhibernate-bugs-submit-good-unit-test.asp%20%20x"&gt;howto&lt;/a&gt; describing how to do it properly.&lt;/p&gt;</description></item><item><title>nhibernate 3.0</title><link>http://nhforge.org/wikis/howtonh/nhibernate-3-0/revision/0.aspx</link><pubDate>Wed, 09 Mar 2011 12:48:32 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:87</guid><dc:creator>olfonur</dc:creator><description>Current revision posted to How to by olfonur on 09/03/2011 09:48:32 a.m.&lt;br /&gt;
&lt;p&gt;nhibertbate&lt;/p&gt;</description></item><item><title>Enumerating First Level Cache Entities</title><link>http://nhforge.org/wikis/howtonh/enumerating-first-level-cache-entities/revision/0.aspx</link><pubDate>Fri, 04 Mar 2011 11:41:09 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:86</guid><dc:creator>Ricardo Peres</dc:creator><description>Current revision posted to How to by Ricardo Peres on 04/03/2011 08:41:09 a.m.&lt;br /&gt;
&lt;h2&gt;Enumerating First Level Cache Entities&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Cache&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;First&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Level&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Cache&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;How to enumerate local (first level cache) session entities:&lt;/p&gt;
&lt;p&gt;
public static class SessionExtensions&lt;br /&gt;
	{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static IEnumerable&amp;lt;T&amp;gt; Local&amp;lt;T&amp;gt;(this ISession session)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ISessionImplementor impl = session.GetSessionImplementation();&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IPersistenceContext pc = impl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (Object key in pc.EntityEntries.Keys)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (key is T)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return ((T) key);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//sample usage:&lt;br /&gt;
	IEnumerable&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;amp;ly;Post&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;lt;Post&amp;gt;&lt;/span&gt; localPosts = session.Local&amp;lt;Post&amp;gt;();&lt;/p&gt;</description></item><item><title>Enumerating First Level Cache Entities</title><link>http://nhforge.org/wikis/howtonh/enumerating-first-level-cache-entities/revision/1.aspx</link><pubDate>Tue, 01 Mar 2011 15:57:58 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:363</guid><dc:creator>Ricardo Peres</dc:creator><description>Revision 1 posted to How to by Ricardo Peres on 01/03/2011 12:57:58 p.m.&lt;br /&gt;
&lt;p&gt;How to enumerate local (first level cache) session entities:&lt;/p&gt;
&lt;p&gt;
public static class SessionExtensions&lt;br /&gt;
	{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static IEnumerable&amp;lt;T&amp;gt; Local&amp;lt;T&amp;gt;(this ISession session)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ISessionImplementor impl = session.GetSessionImplementation();&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IPersistenceContext pc = impl.PersistenceContext;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (Object key in pc.EntityEntries.Keys)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (key is T)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return ((T) key);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//sample usage:&lt;br /&gt;
	IEnumerable&amp;amp;ly;Post&amp;gt; localPosts = session.Local&amp;lt;Post&amp;gt;();&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/0.aspx</link><pubDate>Mon, 27 Sep 2010 08:16:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:82</guid><dc:creator>Matthias Gernand</dc:creator><description>Current revision posted to How to by Matthias Gernand on 27/09/2010 05:16:41 a.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;logging&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;nlog&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;common.logging&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ilogger&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ILogger&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;IInternalLogger&amp;quot;&lt;/span&gt; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. An implementantion can be found&lt;br /&gt;on Codeplex: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. As some of you might know there&lt;br /&gt;is the &lt;a href="http://netcommon.sourceforge.net/"&gt;Common.Logging&lt;/a&gt; logging abstration framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly and the &lt;br /&gt;corresponding Common.Logging-Provider Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;The Common.Logging providers source code and binaries can be found on Codeplex:&lt;br /&gt; &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. You may try out the provider and send me feedback about it. Have fun. [:)]&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/10.aspx</link><pubDate>Fri, 27 Aug 2010 09:02:55 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:349</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 10 posted to How to by Matthias Gernand on 27/08/2010 06:02:55 a.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log, nlog, common.logging, ilogger&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. An implementantion can be found&lt;br /&gt;on Codeplex: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. As some of you might know there&lt;br /&gt;is the &lt;a href="http://netcommon.sourceforge.net/"&gt;Common.Logging&lt;/a&gt; logging abstration framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;corresponding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Common.Logging-Provider&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Assembly&lt;/span&gt; too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;The Common.Logging providers source code and binaries can be found on Codeplex:&lt;br /&gt; &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. You may try out the provider and send me feedback about it. Have fun. [:)]&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/9.aspx</link><pubDate>Mon, 16 Aug 2010 15:14:29 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:344</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 9 posted to How to by Matthias Gernand on 16/08/2010 12:14:29 p.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log, nlog, common.logging, ilogger&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. An implementantion can be found&lt;br /&gt;on Codeplex: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. As some of you might know there&lt;br /&gt;is the &lt;a href="http://netcommon.sourceforge.net/"&gt;Common.Logging&lt;/a&gt; logging abstration framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;The Common.Logging providers source code and binaries can be found &lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Codeplex&lt;/span&gt;:&lt;br /&gt; &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. You may try out the provider and send me feedback about it. Have fun. [:)]&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/8.aspx</link><pubDate>Mon, 16 Aug 2010 15:07:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:342</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 8 posted to How to by Matthias Gernand on 16/08/2010 12:07:17 p.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;nlog&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;common.logging&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ilogger&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;The NHibernate 3 &lt;span style="background: SpringGreen;"&gt;Trunk-Version&lt;/span&gt; contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. An implementantion can be found&lt;br /&gt;on Codeplex: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. As some of you might know there&lt;br /&gt;is the &lt;a href="http://netcommon.sourceforge.net/"&gt;Common.Logging&lt;/a&gt; logging abstration framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;The Common.Logging providers source code and binaries can be found here: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;may&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;try&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;out&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provider&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;send&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;me&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;feedback&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;fun&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:)]&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/7.aspx</link><pubDate>Mon, 16 Aug 2010 15:06:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:341</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 7 posted to How to by Fabio Maulo on 16/08/2010 12:06:22 p.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log&lt;/div&gt;

&lt;p&gt;The NHibernate 3 &lt;span style="text-decoration: line-through; color: red;"&gt;Trunk-Version&lt;/span&gt; contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. An implementantion can be found&lt;br /&gt;on Codeplex: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;. As some of you might know there&lt;br /&gt;is the &lt;a href="http://netcommon.sourceforge.net/"&gt;Common.Logging&lt;/a&gt; logging abstration framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;The Common.Logging providers source code and binaries can be found here: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/6.aspx</link><pubDate>Mon, 16 Aug 2010 15:04:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:340</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 6 posted to How to by Matthias Gernand on 16/08/2010 12:04:02 p.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. &lt;span style="background: SpringGreen;"&gt;An&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implementantion&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Codeplex&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;a href="http://nhlogging.codeplex.com/"&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Logging&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; As some of you might know there&lt;br /&gt;is the &lt;a href="http://netcommon.sourceforge.net/"&gt;Common.Logging&lt;/a&gt; logging &lt;span style="text-decoration: line-through; color: red;"&gt;abstrationb&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;abstration&lt;/span&gt; framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;The Common.Logging providers source code and binaries can be found here: &lt;a href="http://nhlogging.codeplex.com/"&gt;NHibernate.Logging&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/5.aspx</link><pubDate>Mon, 16 Aug 2010 15:02:21 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:339</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 5 posted to How to by Matthias Gernand on 16/08/2010 12:02:21 p.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. As some of you might know there&lt;br /&gt;is the Common.Logging logging abstrationb framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Common.Logging&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;providers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;source&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;binaries&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt; &lt;a href="http://nhlogging.codeplex.com/"&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Logging&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/4.aspx</link><pubDate>Mon, 16 Aug 2010 14:58:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:338</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 4 posted to How to by Matthias Gernand on 16/08/2010 11:58:22 a.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;you are done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. As some of you might know there&lt;br /&gt;is the Common.Logging logging abstrationb framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory, Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/3.aspx</link><pubDate>Mon, 16 Aug 2010 14:58:05 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:337</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 3 posted to How to by Matthias Gernand on 16/08/2010 11:58:05 a.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;your&lt;/span&gt; &lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. As some of you might know there&lt;br /&gt;is the Common.Logging logging abstrationb framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory,  Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/2.aspx</link><pubDate>Mon, 16 Aug 2010 14:57:27 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:336</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 2 posted to How to by Matthias Gernand on 16/08/2010 11:57:27 a.m.&lt;br /&gt;
&lt;h2&gt;Using NLog via Common.Logging with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate, logging, log&lt;/div&gt;

&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;your done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. As some of you might know there&lt;br /&gt;is the Common.Logging logging abstrationb framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory,  Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Using NLog via Common.Logging with NHibernate</title><link>http://nhforge.org/wikis/howtonh/using-nlog-via-common-logging-with-nhibernate/revision/1.aspx</link><pubDate>Mon, 16 Aug 2010 14:56:45 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:335</guid><dc:creator>Matthias Gernand</dc:creator><description>Revision 1 posted to How to by Matthias Gernand on 16/08/2010 11:56:45 a.m.&lt;br /&gt;
&lt;p&gt;The NHibernate 3 Trunk-Version contains a&amp;nbsp; new Interface &amp;quot;ILogger&amp;quot; which provides&lt;br /&gt;interface for custom loggers to be configured with NHibernate. NHibernate provides&amp;#39;&lt;br /&gt;a log4net log provider for this interface out of the box. So if you are using NHibernate&lt;br /&gt;with log4net just deliver the log4net Assembly as you would with NHibernate 2.x and&lt;br /&gt;your done. [:)]&lt;/p&gt;
&lt;p&gt;But if you need or want to use an other logging framework, like NLog, you can provide&lt;br /&gt;an ILogger implementation for your preferred logger. As some of you might know there&lt;br /&gt;is the Common.Logging logging abstrationb framework available. Common.Logging&lt;br /&gt;supports several other logging frameworks. So you can use each of them with NHibernate &lt;br /&gt;via Common.Logging. &lt;br /&gt;&lt;br /&gt;The following logging frameworks are supported by Common.Logging:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;    System.Console&lt;/li&gt;
&lt;li&gt;    System.Diagnostics.Trace&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.9&lt;/li&gt;
&lt;li&gt;Log4Net 1.2.10&lt;/li&gt;
&lt;li&gt;NLog&lt;/li&gt;
&lt;li&gt;Enterprise Library 3.1 Logging&lt;/li&gt;
&lt;li&gt;Enterprise Library 4.1 Logging&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To use the Common.Logging 2.0 logging abstraction framework with NHibernate all you &lt;br /&gt;have to do is to copy the following Assemblies to you projects output directory:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Common.Logging.dll&lt;/li&gt;
&lt;li&gt;NHibernate.Logging.CommonLogging.dll&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Needless to say that you have to copy your loggers Assembly too.&lt;br /&gt;&lt;br /&gt;To enable the log provider you have to add the following lines to your App.config:&lt;/p&gt;
&lt;pre&gt;&amp;lt;appSettings&amp;gt;&lt;br /&gt;	&amp;lt;add key=&amp;quot;nhibernate-logger&amp;quot; &lt;br /&gt;             value=&amp;quot;NHibernate.Logging.CommonLogging.CommonLoggingLoggerFactory,  Hibernate.Logging.CommonLogging&amp;quot;/&amp;gt;&lt;br /&gt;&amp;lt;/appSettings&amp;gt;
&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Keeping Entity State Over Multiple NHibernate Sessions</title><link>http://nhforge.org/wikis/howtonh/keeping-entity-state-over-multiple-nhibernate-sessions/revision/0.aspx</link><pubDate>Sat, 11 Sep 2010 08:57:32 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:83</guid><dc:creator>nadavsof</dc:creator><description>Current revision posted to How to by nadavsof on 11/09/2010 05:57:32 a.m.&lt;br /&gt;
&lt;h2&gt;Keeping Entity State Over Multiple NHibernate Sessions&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Session&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Architecture&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Interceptor&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" align="center" dir="RTL"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;Why would you want to use this source?&lt;/span&gt;&lt;/strong&gt; You&amp;#39;ll want to use
this source if your using NHibernate and need your entities to maintain their
state through long conversations, over multiple NHibernate sessions. You might
also want to use it if you want built in auditing power and general state
oriented methods implemented once and kept through the help of nhibernate.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The problem:&lt;/span&gt;&lt;/strong&gt; NHibernate keeps track of entities within an
NHibernate session (ISession). This means that if you fetch entities within a
session, change those entities and flush the session, the session would know
exactly what you changed, which works very well in most cases &amp;ndash; when it is
possible to re-fetch an entity within any new session, but sometimes this isn&amp;#39;t
enough. Example: multiple views that modifying the same entity instance, when
any one of the views has a &lt;strong&gt;Save&lt;/strong&gt; button, which is the time that the
entity will actually be saved to the DB. In this case, we can&amp;#39;t re-fetch the
entity because it was changed and the DB (or cache) version is irrelevant.
People will tell you &amp;ndash; use a DTO, well, sometimes you gotta use a DTO (when
serializing for example), but here, if we could just modify our previously
fetched entity (it was fetched within an already disposed session), each time
attaching it to the current session (to enable lazy loading), and at the end
saving the changes with nhibernate knowing exactly what was changed (for
dynamic-update for example), wouldn&amp;#39;t it be great?&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The solution - highlevel:&lt;/span&gt;&lt;/strong&gt; Using dynamic proxies that keep
the state of the entities. This way the entities are ignorant of their state
being kept, while the proxies are in charge of keeping the entities&amp;#39; state, and
the NHibernate session interceptor is in charge of using those proxies, to find
out the previous state of an entity, so it is kept through multiple sessions.
The only catch &amp;ndash; each entity, when being instantiated, must be wrapped with a
proxy.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The usage:&lt;/span&gt;&lt;/strong&gt; 2 main classes concern the End-Developer:&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;1.NHibernateInterceptor&lt;/span&gt; &amp;ndash; Implementor of the &lt;span&gt;NHibernate.&lt;span&gt;IInterceptor &lt;/span&gt;&lt;/span&gt;&lt;span&gt;interface, which &lt;/span&gt;intercept certain events of the session (like &lt;strong&gt;FindDirty).&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;How should it be used?&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;/strong&gt;When creating a session, the interceptor should be injected into the
session. The interceptor also gets the session factory as a c&amp;#39;tor argument. The
following code demonstrates its usage:&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;ISession&lt;/span&gt;&lt;span&gt; session =&amp;nbsp;&lt;/span&gt;sessionFactory.OpenSession(&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;NHibernateInterceptor&lt;/span&gt;(sessionFactory))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;2.StatefullEntityProxyFactory&lt;/span&gt; &amp;ndash; A factory to be used
when instantiating an entity. This class is responsible for creating a proxy
from an existing, or non existing entity instance. The proxy will appear to the
application as if it is the actual entity, with one exception &amp;ndash; the &lt;strong&gt;GetType()&lt;/strong&gt;
method. The proxy, although appears as the actual entity, can also be looked at
as the statefull entity proxy.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;How should it be used?&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;i&gt;Instantiating an entity with a default 0-params c&amp;#39;tor:&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&lt;/i&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy&amp;lt;&lt;span&gt;Entity&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&amp;hellip;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&amp;lt;Set values, do stuff&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&amp;hellip;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&lt;/i&gt;session.SaveOrUpdate(entity);&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;Instantiating a proxy on an existing entity:&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity = new Entity(1,&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;name&amp;quot;);&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;name&amp;quot;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy(entity)
as Entity;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;The above two examples are the use cases for instantiating a proxy
instead of an entity, so that its state will be kept. These examples are shown
for creating new entities. When an entity is fetched with NHibernate, the
NHibernateInterceptor will do the same, so that the state will be kept for the
nhibernate fetched entities.&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Ok, but why does it work?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li class="MsoNormal" dir="LTR"&gt;The &lt;span&gt;StatefullEntityProxyInterceptor&lt;/span&gt;
     class is a LinFu dynamic proxies interceptor, which holds a reference to a
     &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt; instance and to an entity. It forwards all the
     calls (properties and methods) to the underlying entity, except for a call
     to get the &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt;. This call is possible if we do: 
     
     &lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;StateKeerp &lt;/span&gt;&lt;span&gt;stateKeeper
= proxy.StateKeeper;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li class="MsoNormal" dir="LTR"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; &lt;/span&gt;is
     an interface we&amp;#39;re making the proxy implement (using the LinFu proxies
     API), and that defines a property of type &lt;strong&gt;&lt;i&gt;StateKeeper&lt;br /&gt;
     &lt;br /&gt;
     &lt;/i&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li class="MsoNormal" dir="LTR"&gt;The &lt;span&gt;StateKeeper&lt;/span&gt; class has
     a previous state and a current state, and implements a FindDirty method,
     that finds the dirty properties (by comparing the previous and current
     states). The nhibernate interceptor, in its &lt;strong&gt;OnLoad&lt;/strong&gt; event will set
     the previous state of the entity, and in the &lt;strong&gt;PostFlush&lt;/strong&gt; it will call
     the &lt;strong&gt;InitState &lt;/strong&gt;method of the state keeper. In the &lt;strong&gt;FindDirty&lt;/strong&gt;,
     it will of course use the FindDirty method. Example (from the OnLoad
     event): &lt;br /&gt;
     &lt;br /&gt;
     &lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&lt;br /&gt;
     proxy.StateKeeper.InitState(propertyNames, state);&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;To fully understand how it works, it is recommended that you setup a
small application with a couple of entities, map them with NHibernate,
Instantiate them with the above described proxy factory, create the session
injecting the interceptor, put breakpoints in every method of the
NHibernateInterceptor and watch what&amp;#39;s going on when you
save\update\fetch\attach your entities to a different session.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;nbsp;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;What else can be done with this idea?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;Well,&lt;/span&gt; &lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;Well&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; lots of stuff. An example of a generic IsModified method is there
&amp;ndash; just get the underlying StateKeeper as shown in the examples above, and call
IsModified. FindDirty, which returns the dirty property names can be useful for
auditing as well, and the idea of IsModified can also be extended to implement
RejectChanges.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;nbsp;&lt;strong&gt;Technologies used: &lt;/strong&gt;NHibernate 3.0 Alpha2, LinFu dynamic proxies.
The source what not tested against NHibernate 2.1, but should work as no NH3 specific
feature is being used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;The source:&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interface for a statefull proxy - a proxy for some entity that also holds a state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// that manages the state&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public interface IStatefullProxy&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StateKeeper StateKeeper { get; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Keeps the state of an entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StateKeeper&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StateKeeper(object entity)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The previous state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; previousState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Set the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted properties&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The dictionary to populate&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void SetState(string[] properties, object[] values, IDictionary&amp;lt;string, object&amp;gt; state)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; properties.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;state[properties[i]] = values[i];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets weather the entity is a new one&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsNew { get; protected set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = this.currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper with the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetCurrentState(properties, values);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.InitState();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the current state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetCurrentState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.currentState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the previous state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetPreviousState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.previousState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty property names&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Enumeration of the dirty property names&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IEnumerable&amp;lt;string&amp;gt; FindDirty()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsNew)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new string[0];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.currentState == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;string&amp;gt; dirtyProperties = new List&amp;lt;string&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (KeyValuePair&amp;lt;string, object&amp;gt; property in this.previousState)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object previousValue = this.previousState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object newValue = this.currentState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsChanged(previousValue, newValue))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dirtyProperties.Add(property.Key);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return dirtyProperties;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Check weather a specific property value is dirty&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;previousValue&amp;quot;&amp;gt;previous value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;newValue&amp;quot;&amp;gt;new value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;true if its dirty&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private bool IsChanged(object previousValue, object newValue)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!NHibernateUtil.IsInitialized(previousValue) &amp;amp;&amp;amp; !(NHibernateUtil.IsInitialized(newValue)))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue is AbstractPersistentCollection)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (previousValue as AbstractPersistentCollection).IsDirty;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (previousValue == null &amp;amp;&amp;amp; newValue == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue != null &amp;amp;&amp;amp; newValue != null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return !previousValue.Equals(newValue);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// one of them is null and the other isn&amp;#39;t&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties corresponding indexes&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;propertyNames&amp;quot;&amp;gt;The property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The indexes of the dirty properties&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int[] FindDirtyIndexes(string[] propertyNames)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IEnumerable&amp;lt;string&amp;gt; dirtyProperties = this.FindDirty();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;int&amp;gt; indexes = new List&amp;lt;int&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; propertyNames.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties.Contains(propertyNames[i]))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;indexes.Add(i);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return indexes.ToArray();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsModified&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.FindDirty().Any())&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void ReadStateFromEntity()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (string propertyName in this.previousState.Keys)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MemberInfo member =this.entity.GetType().GetMember(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;propertyName,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.First();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (member is PropertyInfo)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((PropertyInfo)member).GetValue(this.entity, null);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((FieldInfo)member).GetValue(this.entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// A Statefull entity proxy that is constructed from an entity, forwarding all the calls to it&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// and a statekeeper that can be retrieved by the IStatefullProxy interface&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyInterceptor : global::LinFu.DynamicProxy.IInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private StateKeeper stateKeeper;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiates the proxy with a given entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity that its state will be managed by this proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StatefullEntityProxyInterceptor(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.stateKeeper = new StateKeeper(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Part of the linfu interceptor interface. This method is called for each virtual property or method call on the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;info&amp;quot;&amp;gt;The invocation info on the current call&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Return the return value of the method/property call&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public object Intercept(LinFu.DynamicProxy.InvocationInfo info)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If its a call to get the state keeper, return the proxy&amp;#39;s underlying state keeper&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (info.TargetMethod.Name == &amp;quot;get_StateKeeper&amp;quot;)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.stateKeeper;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// else, forward the call to the actual entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return info.TargetMethod.Invoke(this.entity, info.Arguments);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int GetHashCode()&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.GetHashCode();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool Equals(object obj)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.Equals(obj);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Factory for generating entity proxies&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyFactory&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static readonly global::LinFu.DynamicProxy.ProxyFactory factory = &lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new global::LinFu.DynamicProxy.ProxyFactory();&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy from an existing entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity to proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxied entity&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Creates a new proxy interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StatefullEntityProxyInterceptor stateKeeperProxy = new StatefullEntityProxyInterceptor(entity);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Generate a proxy, with the statefull entity interceptor as the interceptor, and the entity type as the derrived type&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (IStatefullProxy)factory.CreateProxy(entity.GetType(), stateKeeperProxy, typeof(IStatefullProxy));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;objectType&amp;quot;&amp;gt;Type to create a proxy for&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The generated proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(Type objectType)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object objectToProxy = objectType.GetConstructor(new Type[0]).Invoke(new object[0]);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.InstantiateEntityProxy(objectToProxy);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;typeparam name=&amp;quot;TEntity&amp;quot;&amp;gt;The type to craete proxy for&amp;lt;/typeparam&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public TEntity InstantiateEntityProxy&amp;lt;TEntity&amp;gt;() where TEntity : class&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (TEntity)(this.InstantiateEntityProxy(typeof(TEntity)));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interceptor that intercept events of the session that it is binds to, and assumes that all entities are actually&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// statefull entity proxies, and uses the underlying state keeper to determine the previous state of entities&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// so that the state is kepts even through different sessions&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class NHibernateInterceptor : EmptyInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;sessionFactory&amp;quot;&amp;gt;The session factory that is used to generate the current session&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public NHibernateInterceptor(ISessionFactory sessionFactory)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Factory = sessionFactory as ISessionFactoryImplementor;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private ISessionFactoryImplementor Factory {get;set;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new entity instance using the statefull entity proxy factory&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;The call to this method is made by nhibernate when loading entities from queries&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override object Instantiate(string clazz, EntityMode entityMode, object id)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Get the type of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Type type = Type.GetType(clazz);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a new instance of the entity using the persister of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object entity = this.Factory.GetEntityPersister(clazz).Instantiate(id, entityMode);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a proxy from the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object proxy = new StatefullEntityProxyFactory().InstantiateEntityProxy(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiate the state of the proxy when an entity is loaded&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;This happens after the instantiation of an entity (a proxy)&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState(propertyNames, state);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return base.OnLoad(entity, id, state, propertyNames, types);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties of the entity. Uses the proxy get the dirty properties&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;When updating an entity in a different session, the previousState argument would be null!!&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.SetCurrentState(propertyNames, currentState);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.FindDirtyIndexes(propertyNames);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Checks weather the given entity is transient using the proxy.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool? IsTransient(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.IsNew; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// After a flush of an entity, the init state must be called, because the current Current value is actually the new previous value&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void PostFlush(System.Collections.ICollection entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (IStatefullProxy proxy in entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets the entity name&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;Because the entity is actually a proxy, we gotta return the base type of the entity, which is the actual entity type&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override string GetEntityName(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return entity.GetType().BaseType.FullName;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Keeping Entity State Over Multiple NHibernate Sessions</title><link>http://nhforge.org/wikis/howtonh/keeping-entity-state-over-multiple-nhibernate-sessions/revision/3.aspx</link><pubDate>Sat, 11 Sep 2010 08:54:47 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:347</guid><dc:creator>nadavsof</dc:creator><description>Revision 3 posted to How to by nadavsof on 11/09/2010 05:54:47 a.m.&lt;br /&gt;
&lt;h2&gt;Keeping Entity State Over Multiple NHibernate Sessions&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture, Nhibernate, Interceptor&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p dir="RTL" align="center" class="MsoNormal"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;Why would you want to use this source?&lt;/span&gt;&lt;/strong&gt; You&amp;#39;ll want to use
this source if your using NHibernate and need your entities to maintain their
state through long conversations, over multiple NHibernate sessions. You might
also want to use it if you want built in auditing power and general state
oriented methods implemented once and kept through the help of nhibernate.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The problem:&lt;/span&gt;&lt;/strong&gt; NHibernate keeps track of entities within an
NHibernate session (ISession). This means that if you fetch entities within a
session, change those entities and flush the session, the session would know
exactly what you changed, which works very well in most cases &amp;ndash; when it is
possible to re-fetch an entity within any new session, but sometimes this isn&amp;#39;t
enough. Example: multiple views that modifying the same entity instance, when
any one of the views has a &lt;strong&gt;Save&lt;/strong&gt; button, which is the time that the
entity will actually be saved to the DB. In this case, we can&amp;#39;t re-fetch the
entity because it was changed and the DB (or cache) version is irrelevant.
People will tell you &amp;ndash; use a DTO, well, sometimes you gotta use a DTO (when
serializing for example), but here, if we could just modify our previously
fetched entity (it was fetched within an already disposed session), each time
attaching it to the current session (to enable lazy loading), and at the end
saving the changes with nhibernate knowing exactly what was changed (for
dynamic-update for example), wouldn&amp;#39;t it be great?&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The solution - highlevel:&lt;/span&gt;&lt;/strong&gt; Using dynamic proxies that keep
the state of the entities. This way the entities are ignorant of their state
being kept, while the proxies are in charge of keeping the entities&amp;#39; state, and
the NHibernate session interceptor is in charge of using those proxies, to find
out the previous state of an entity, so it is kept through multiple sessions.
The only catch &amp;ndash; each entity, when being instantiated, must be wrapped with a
proxy.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The usage:&lt;/span&gt;&lt;/strong&gt; 2 main classes concern the End-Developer:&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;1.NHibernateInterceptor&lt;/span&gt; &amp;ndash; Implementor of the &lt;span&gt;NHibernate.&lt;span&gt;IInterceptor &lt;/span&gt;&lt;/span&gt;&lt;span&gt;interface, which &lt;/span&gt;intercept certain events of the session (like &lt;strong&gt;FindDirty).&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;How should it be used?&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;/strong&gt;When creating a session, the interceptor should be injected into the
session. The interceptor also gets the session factory as a c&amp;#39;tor argument. The
following code demonstrates its usage:&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;ISession&lt;/span&gt;&lt;span&gt; session =&amp;nbsp;&lt;/span&gt;sessionFactory.OpenSession(&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;NHibernateInterceptor&lt;/span&gt;(sessionFactory))&lt;/span&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;2.StatefullEntityProxyFactory&lt;/span&gt; &amp;ndash; A factory to be used
when instantiating an entity. This class is responsible for creating a proxy
from an existing, or non existing entity instance. The proxy will appear to the
application as if it is the actual entity, with one exception &amp;ndash; the &lt;strong&gt;GetType()&lt;/strong&gt;
method. The proxy, although appears as the actual entity, can also be looked at
as the statefull entity proxy.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;How should it be used?&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;i&gt;Instantiating an entity with a default 0-params c&amp;#39;tor:&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&lt;/i&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy&amp;lt;&lt;span&gt;Entity&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;hellip;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&lt;span style="background: SpringGreen;"&gt;&amp;hellip;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&amp;lt;Set values, do stuff&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;hellip;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&lt;/i&gt;&lt;i&gt;&lt;span style="background: SpringGreen;"&gt;&amp;hellip;&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&lt;/i&gt;session.SaveOrUpdate(entity);&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;Instantiating a proxy on an existing entity:&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity = new Entity(1,&amp;quot;name&amp;quot;);&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy(entity)
as Entity;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;The above two examples are the use cases for instantiating a proxy
instead of an entity, so that its state will be kept. These examples are shown
for creating new entities. When an entity is fetched with NHibernate, the
NHibernateInterceptor will do the same, so that the state will be kept for the
nhibernate fetched entities.&amp;nbsp;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Ok, but why does it work?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li dir="LTR" class="MsoNormal"&gt;The &lt;span&gt;StatefullEntityProxyInterceptor&lt;/span&gt;
     class is a LinFu dynamic proxies interceptor, which holds a reference to a
     &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt; instance and to an entity. It forwards all the
     calls (properties and methods) to the underlying entity, except for a call
     to get the &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt;. This call is possible if we do: 
     
     &lt;/li&gt;
&lt;/ol&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;StateKeerp &lt;/span&gt;&lt;span&gt;stateKeeper
= proxy.StateKeeper;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li dir="LTR" class="MsoNormal"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; &lt;/span&gt;is
     an interface we&amp;#39;re making the proxy implement (using the LinFu proxies
     API), and that defines a property of type &lt;strong&gt;&lt;i&gt;StateKeeper&lt;br /&gt;
     &lt;br /&gt;
     &lt;/i&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li dir="LTR" class="MsoNormal"&gt;The &lt;span&gt;StateKeeper&lt;/span&gt; class has
     a previous state and a current state, and implements a FindDirty method,
     that finds the dirty properties (by comparing the previous and current
     states). The nhibernate interceptor, in its &lt;strong&gt;OnLoad&lt;/strong&gt; event will set
     the previous state of the entity, and in the &lt;strong&gt;PostFlush&lt;/strong&gt; it will call
     the &lt;strong&gt;InitState &lt;/strong&gt;method of the state keeper. In the &lt;strong&gt;FindDirty&lt;/strong&gt;,
     it will of course use the FindDirty method. Example (from the OnLoad
     event): &lt;br /&gt;
     &lt;br /&gt;
     &lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&lt;br /&gt;
     proxy.StateKeeper.InitState(propertyNames, state);&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;To fully understand how it works, it is recommended that you setup a
small application with a couple of entities, map them with NHibernate,
Instantiate them with the above described proxy factory, create the session
injecting the interceptor, put breakpoints in every method of the
NHibernateInterceptor and watch what&amp;#39;s going on when you
save\update\fetch\attach your entities to a different session.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;What else can be done with this idea?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;Well, lots of stuff. An example of a generic IsModified method is there
&amp;ndash; just get the underlying StateKeeper as shown in the examples above, and call
IsModified. FindDirty, which returns the dirty property names can be useful for
auditing as well, and the idea of IsModified can also be extended to implement
RejectChanges.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;strong&gt;Technologies used: &lt;/strong&gt;NHibernate 3.0 Alpha2, LinFu dynamic proxies.
The source what not tested against NHibernate 2.1, but should work as no NH3 specific
feature is being used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;The source:&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interface for a statefull proxy - a proxy for some entity that also holds a state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// that manages the state&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public interface IStatefullProxy&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StateKeeper StateKeeper { get; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Keeps the state of an entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StateKeeper&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StateKeeper(object entity)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The previous state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; previousState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Set the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted properties&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The dictionary to populate&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void SetState(string[] properties, object[] values, IDictionary&amp;lt;string, object&amp;gt; state)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; properties.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;state[properties[i]] = values[i];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets weather the entity is a new one&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsNew { get; protected set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = this.currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper with the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetCurrentState(properties, values);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.InitState();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the current state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetCurrentState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.currentState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the previous state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetPreviousState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.previousState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty property names&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Enumeration of the dirty property names&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IEnumerable&amp;lt;string&amp;gt; FindDirty()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsNew)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new string[0];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.currentState == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;string&amp;gt; dirtyProperties = new List&amp;lt;string&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (KeyValuePair&amp;lt;string, object&amp;gt; property in this.previousState)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object previousValue = this.previousState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object newValue = this.currentState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsChanged(previousValue, newValue))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dirtyProperties.Add(property.Key);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return dirtyProperties;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Check weather a specific property value is dirty&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;previousValue&amp;quot;&amp;gt;previous value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;newValue&amp;quot;&amp;gt;new value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;true if its dirty&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private bool IsChanged(object previousValue, object newValue)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!NHibernateUtil.IsInitialized(previousValue) &amp;amp;&amp;amp; !(NHibernateUtil.IsInitialized(newValue)))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue is AbstractPersistentCollection)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (previousValue as AbstractPersistentCollection).IsDirty;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (previousValue == null &amp;amp;&amp;amp; newValue == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue != null &amp;amp;&amp;amp; newValue != null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return !previousValue.Equals(newValue);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// one of them is null and the other isn&amp;#39;t&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties corresponding indexes&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;propertyNames&amp;quot;&amp;gt;The property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The indexes of the dirty properties&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int[] FindDirtyIndexes(string[] propertyNames)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IEnumerable&amp;lt;string&amp;gt; dirtyProperties = this.FindDirty();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;int&amp;gt; indexes = new List&amp;lt;int&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; propertyNames.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties.Contains(propertyNames[i]))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;indexes.Add(i);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return indexes.ToArray();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsModified&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.FindDirty().Any())&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void ReadStateFromEntity()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (string propertyName in this.previousState.Keys)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MemberInfo member =this.entity.GetType().GetMember(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;propertyName,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.First();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (member is PropertyInfo)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((PropertyInfo)member).GetValue(this.entity, null);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((FieldInfo)member).GetValue(this.entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// A Statefull entity proxy that is constructed from an entity, forwarding all the calls to it&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// and a statekeeper that can be retrieved by the IStatefullProxy interface&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyInterceptor : global::LinFu.DynamicProxy.IInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private StateKeeper stateKeeper;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiates the proxy with a given entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity that its state will be managed by this proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StatefullEntityProxyInterceptor(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.stateKeeper = new StateKeeper(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Part of the linfu interceptor interface. This method is called for each virtual property or method call on the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;info&amp;quot;&amp;gt;The invocation info on the current call&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Return the return value of the method/property call&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public object Intercept(LinFu.DynamicProxy.InvocationInfo info)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If its a call to get the state keeper, return the proxy&amp;#39;s underlying state keeper&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (info.TargetMethod.Name == &amp;quot;get_StateKeeper&amp;quot;)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.stateKeeper;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// else, forward the call to the actual entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return info.TargetMethod.Invoke(this.entity, info.Arguments);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int GetHashCode()&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.GetHashCode();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool Equals(object obj)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.Equals(obj);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Factory for generating entity proxies&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyFactory&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static readonly global::LinFu.DynamicProxy.ProxyFactory factory = &lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new global::LinFu.DynamicProxy.ProxyFactory();&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy from an existing entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity to proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxied entity&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Creates a new proxy interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StatefullEntityProxyInterceptor stateKeeperProxy = new StatefullEntityProxyInterceptor(entity);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Generate a proxy, with the statefull entity interceptor as the interceptor, and the entity type as the derrived type&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (IStatefullProxy)factory.CreateProxy(entity.GetType(), stateKeeperProxy, typeof(IStatefullProxy));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;objectType&amp;quot;&amp;gt;Type to create a proxy for&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The generated proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(Type objectType)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object objectToProxy = objectType.GetConstructor(new Type[0]).Invoke(new object[0]);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.InstantiateEntityProxy(objectToProxy);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;typeparam name=&amp;quot;TEntity&amp;quot;&amp;gt;The type to craete proxy for&amp;lt;/typeparam&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public TEntity InstantiateEntityProxy&amp;lt;TEntity&amp;gt;() where TEntity : class&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (TEntity)(this.InstantiateEntityProxy(typeof(TEntity)));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interceptor that intercept events of the session that it is binds to, and assumes that all entities are actually&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// statefull entity proxies, and uses the underlying state keeper to determine the previous state of entities&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// so that the state is kepts even through different sessions&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class NHibernateInterceptor : EmptyInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;sessionFactory&amp;quot;&amp;gt;The session factory that is used to generate the current session&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public NHibernateInterceptor(ISessionFactory sessionFactory)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Factory = sessionFactory as ISessionFactoryImplementor;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private ISessionFactoryImplementor Factory {get;set;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new entity instance using the statefull entity proxy factory&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;The call to this method is made by nhibernate when loading entities from queries&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override object Instantiate(string clazz, EntityMode entityMode, object id)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Get the type of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Type type = Type.GetType(clazz);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a new instance of the entity using the persister of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object entity = this.Factory.GetEntityPersister(clazz).Instantiate(id, entityMode);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a proxy from the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object proxy = new StatefullEntityProxyFactory().InstantiateEntityProxy(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiate the state of the proxy when an entity is loaded&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;This happens after the instantiation of an entity (a proxy)&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState(propertyNames, state);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return base.OnLoad(entity, id, state, propertyNames, types);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties of the entity. Uses the proxy get the dirty properties&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;When updating an entity in a different session, the previousState argument would be null!!&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.SetCurrentState(propertyNames, currentState);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.FindDirtyIndexes(propertyNames);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Checks weather the given entity is transient using the proxy.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool? IsTransient(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.IsNew; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// After a flush of an entity, the init state must be called, because the current Current value is actually the new previous value&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void PostFlush(System.Collections.ICollection entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (IStatefullProxy proxy in entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets the entity name&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;Because the entity is actually a proxy, we gotta return the base type of the entity, which is the actual entity type&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override string GetEntityName(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return entity.GetType().BaseType.FullName;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Keeping Entity State Over Multiple NHibernate Sessions</title><link>http://nhforge.org/wikis/howtonh/keeping-entity-state-over-multiple-nhibernate-sessions/revision/2.aspx</link><pubDate>Sat, 11 Sep 2010 08:53:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:346</guid><dc:creator>nadavsof</dc:creator><description>Revision 2 posted to How to by nadavsof on 11/09/2010 05:53:54 a.m.&lt;br /&gt;
&lt;h2&gt;Keeping Entity State Over Multiple NHibernate Sessions&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, Architecture, Nhibernate, Interceptor&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" align="center" dir="RTL"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;Why would you want to use this source?&lt;/span&gt;&lt;/strong&gt; You&amp;#39;ll want to use
this source if your using NHibernate and need your entities to maintain their
state through long conversations, over multiple NHibernate sessions. You might
also want to use it if you want built in auditing power and general state
oriented methods implemented once and kept through the help of nhibernate.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The problem:&lt;/span&gt;&lt;/strong&gt; NHibernate keeps track of entities within an
NHibernate session (ISession). This means that if you fetch entities within a
session, change those entities and flush the session, the session would know
exactly what you changed, which works very well in most cases &amp;ndash; when it is
possible to re-fetch an entity within any new session, but sometimes this isn&amp;#39;t
enough. Example: multiple views that modifying the same entity instance, when
any one of the views has a &lt;strong&gt;Save&lt;/strong&gt; button, which is the time that the
entity will actually be saved to the DB. In this case, we can&amp;#39;t re-fetch the
entity because it was changed and the DB (or cache) version is irrelevant.
People will tell you &amp;ndash; use a DTO, well, sometimes you gotta use a DTO (when
serializing for example), but here, if we could just modify our previously
fetched entity (it was fetched within an already disposed session), each time
attaching it to the current session (to enable lazy loading), and at the end
saving the changes with nhibernate knowing exactly what was changed (for
dynamic-update for example), wouldn&amp;#39;t it be great?&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The solution - highlevel:&lt;/span&gt;&lt;/strong&gt; Using dynamic proxies that keep
the state of the entities. This way the entities are ignorant of their state
being kept, while the proxies are in charge of keeping the entities&amp;#39; state, and
the NHibernate session interceptor is in charge of using those proxies, to find
out the previous state of an entity, so it is kept through multiple sessions.
The only catch &amp;ndash; each entity, when being instantiated, must be wrapped with a
proxy.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The usage:&lt;/span&gt;&lt;/strong&gt; 2 main classes concern the End-Developer:&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;1.NHibernateInterceptor&lt;/span&gt; &amp;ndash; Implementor of the &lt;span&gt;NHibernate.&lt;span&gt;IInterceptor &lt;/span&gt;&lt;/span&gt;&lt;span&gt;interface, which &lt;/span&gt;intercept certain events of the session (like &lt;strong&gt;FindDirty).&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;How should it be used?&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;When creating a session, the interceptor should be injected into the
session. The interceptor also gets the session factory as a c&amp;#39;tor argument. The
following code demonstrates its usage:&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;ISession&lt;/span&gt;&lt;span&gt; session =&amp;nbsp;&lt;/span&gt;sessionFactory.OpenSession(&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;NHibernateInterceptor&lt;/span&gt;(sessionFactory))&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;2.StatefullEntityProxyFactory&lt;/span&gt; &amp;ndash; A factory to be used
when instantiating an entity. This class is responsible for creating a proxy
from an existing, or non existing entity instance. The proxy will appear to the
application as if it is the actual entity, with one exception &amp;ndash; the &lt;strong&gt;GetType()&lt;/strong&gt;
method. The proxy, although appears as the actual entity, can also be looked at
as the statefull entity proxy.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;How should it be used?&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;Instantiating an entity with a default 0-params c&amp;#39;tor:&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy&amp;lt;&lt;span&gt;Entity&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&amp;hellip;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&amp;lt;Set values, do stuff&amp;gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;&amp;hellip;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;session.SaveOrUpdate(entity);&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;i&gt;Instantiating a proxy on an existing entity:&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity = new Entity(1,&amp;quot;name&amp;quot;);&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy(entity)
as Entity;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;The above two examples are the use cases for instantiating a proxy
instead of an entity, so that its state will be kept. These examples are shown
for creating new entities. When an entity is fetched with NHibernate, the
NHibernateInterceptor will do the same, so that the state will be kept for the
nhibernate fetched entities.&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Ok, but why does it work?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li class="MsoNormal" dir="LTR"&gt;The &lt;span&gt;StatefullEntityProxyInterceptor&lt;/span&gt;
     class is a LinFu dynamic proxies interceptor, which holds a reference to a
     &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt; instance and to an entity. It forwards all the
     calls (properties and methods) to the underlying entity, except for a call
     to get the &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt;. This call is possible if we do: 
     
     &lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&lt;span&gt;StateKeerp &lt;/span&gt;&lt;span&gt;stateKeeper
= proxy.StateKeeper;&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li class="MsoNormal" dir="LTR"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; &lt;/span&gt;is
     an interface we&amp;#39;re making the proxy implement (using the LinFu proxies
     API), and that defines a property of type &lt;strong&gt;&lt;i&gt;StateKeeper&lt;br /&gt;
     &lt;br /&gt;
     &lt;/i&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li class="MsoNormal" dir="LTR"&gt;The &lt;span&gt;StateKeeper&lt;/span&gt; class has
     a previous state and a current state, and implements a FindDirty method,
     that finds the dirty properties (by comparing the previous and current
     states). The nhibernate interceptor, in its &lt;strong&gt;OnLoad&lt;/strong&gt; event will set
     the previous state of the entity, and in the &lt;strong&gt;PostFlush&lt;/strong&gt; it will call
     the &lt;strong&gt;InitState &lt;/strong&gt;method of the state keeper. In the &lt;strong&gt;FindDirty&lt;/strong&gt;,
     it will of course use the FindDirty method. Example (from the OnLoad
     event): &lt;br /&gt;
     &lt;br /&gt;
     &lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&lt;br /&gt;
     proxy.StateKeeper.InitState(propertyNames, state);&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;To fully understand how it works, it is recommended that you setup a
small application with a couple of entities, map them with NHibernate,
Instantiate them with the above described proxy factory, create the session
injecting the interceptor, put breakpoints in every method of the
NHibernateInterceptor and watch what&amp;#39;s going on when you
save\update\fetch\attach your entities to a different session.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;What else can be done with this idea?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;Well, lots of stuff. An example of a generic IsModified method is there
&amp;ndash; just get the underlying StateKeeper as shown in the examples above, and call
IsModified. FindDirty, which returns the dirty property names can be useful for
auditing as well, and the idea of IsModified can also be extended to implement
RejectChanges.&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;strong&gt;Technologies used: &lt;/strong&gt;NHibernate 3.0 Alpha2, LinFu dynamic proxies.
The source what not tested against NHibernate 2.1, but should work as no NH3 specific
feature is being used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" dir="LTR"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;The source:&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interface for a statefull proxy - a proxy for some entity that also holds a state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// that manages the state&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public interface IStatefullProxy&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StateKeeper StateKeeper { get; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Keeps the state of an entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StateKeeper&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StateKeeper(object entity)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The previous state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; previousState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Set the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted properties&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The dictionary to populate&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void SetState(string[] properties, object[] values, IDictionary&amp;lt;string, object&amp;gt; state)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; properties.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;state[properties[i]] = values[i];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets weather the entity is a new one&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsNew { get; protected set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = this.currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper with the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetCurrentState(properties, values);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.InitState();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the current state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetCurrentState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.currentState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the previous state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetPreviousState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.previousState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty property names&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Enumeration of the dirty property names&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IEnumerable&amp;lt;string&amp;gt; FindDirty()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsNew)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new string[0];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.currentState == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;string&amp;gt; dirtyProperties = new List&amp;lt;string&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (KeyValuePair&amp;lt;string, object&amp;gt; property in this.previousState)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object previousValue = this.previousState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object newValue = this.currentState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsChanged(previousValue, newValue))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dirtyProperties.Add(property.Key);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return dirtyProperties;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Check weather a specific property value is dirty&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;previousValue&amp;quot;&amp;gt;previous value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;newValue&amp;quot;&amp;gt;new value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;true if its dirty&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private bool IsChanged(object previousValue, object newValue)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!NHibernateUtil.IsInitialized(previousValue) &amp;amp;&amp;amp; !(NHibernateUtil.IsInitialized(newValue)))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue is AbstractPersistentCollection)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (previousValue as AbstractPersistentCollection).IsDirty;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (previousValue == null &amp;amp;&amp;amp; newValue == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue != null &amp;amp;&amp;amp; newValue != null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return !previousValue.Equals(newValue);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// one of them is null and the other isn&amp;#39;t&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties corresponding indexes&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;propertyNames&amp;quot;&amp;gt;The property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The indexes of the dirty properties&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int[] FindDirtyIndexes(string[] propertyNames)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IEnumerable&amp;lt;string&amp;gt; dirtyProperties = this.FindDirty();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;int&amp;gt; indexes = new List&amp;lt;int&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; propertyNames.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties.Contains(propertyNames[i]))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;indexes.Add(i);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return indexes.ToArray();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsModified&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.FindDirty().Any())&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void ReadStateFromEntity()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (string propertyName in this.previousState.Keys)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MemberInfo member =this.entity.GetType().GetMember(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;propertyName,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.First();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (member is PropertyInfo)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((PropertyInfo)member).GetValue(this.entity, null);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((FieldInfo)member).GetValue(this.entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// A Statefull entity proxy that is constructed from an entity, forwarding all the calls to it&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// and a statekeeper that can be retrieved by the IStatefullProxy interface&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyInterceptor : global::LinFu.DynamicProxy.IInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private StateKeeper stateKeeper;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiates the proxy with a given entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity that its state will be managed by this proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StatefullEntityProxyInterceptor(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.stateKeeper = new StateKeeper(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Part of the linfu interceptor interface. This method is called for each virtual property or method call on the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;info&amp;quot;&amp;gt;The invocation info on the current call&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Return the return value of the method/property call&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public object Intercept(LinFu.DynamicProxy.InvocationInfo info)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If its a call to get the state keeper, return the proxy&amp;#39;s underlying state keeper&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (info.TargetMethod.Name == &amp;quot;get_StateKeeper&amp;quot;)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.stateKeeper;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// else, forward the call to the actual entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return info.TargetMethod.Invoke(this.entity, info.Arguments);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int GetHashCode()&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.GetHashCode();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool Equals(object obj)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.Equals(obj);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Factory for generating entity proxies&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyFactory&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static readonly global::LinFu.DynamicProxy.ProxyFactory factory = &lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new global::LinFu.DynamicProxy.ProxyFactory();&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy from an existing entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity to proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxied entity&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Creates a new proxy interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StatefullEntityProxyInterceptor stateKeeperProxy = new StatefullEntityProxyInterceptor(entity);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Generate a proxy, with the statefull entity interceptor as the interceptor, and the entity type as the derrived type&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (IStatefullProxy)factory.CreateProxy(entity.GetType(), stateKeeperProxy, typeof(IStatefullProxy));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;objectType&amp;quot;&amp;gt;Type to create a proxy for&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The generated proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(Type objectType)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object objectToProxy = objectType.GetConstructor(new Type[0]).Invoke(new object[0]);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.InstantiateEntityProxy(objectToProxy);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;typeparam name=&amp;quot;TEntity&amp;quot;&amp;gt;The type to craete proxy for&amp;lt;/typeparam&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public TEntity InstantiateEntityProxy&amp;lt;TEntity&amp;gt;() where TEntity : class&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (TEntity)(this.InstantiateEntityProxy(typeof(TEntity)));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interceptor that intercept events of the session that it is binds to, and assumes that all entities are actually&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// statefull entity proxies, and uses the underlying state keeper to determine the previous state of entities&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// so that the state is kepts even through different sessions&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class NHibernateInterceptor : EmptyInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;sessionFactory&amp;quot;&amp;gt;The session factory that is used to generate the current session&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public NHibernateInterceptor(ISessionFactory sessionFactory)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Factory = sessionFactory as ISessionFactoryImplementor;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private ISessionFactoryImplementor Factory {get;set;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new entity instance using the statefull entity proxy factory&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;The call to this method is made by nhibernate when loading entities from queries&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override object Instantiate(string clazz, EntityMode entityMode, object id)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Get the type of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Type type = Type.GetType(clazz);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a new instance of the entity using the persister of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object entity = this.Factory.GetEntityPersister(clazz).Instantiate(id, entityMode);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a proxy from the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object proxy = new StatefullEntityProxyFactory().InstantiateEntityProxy(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiate the state of the proxy when an entity is loaded&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;This happens after the instantiation of an entity (a proxy)&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState(propertyNames, state);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return base.OnLoad(entity, id, state, propertyNames, types);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties of the entity. Uses the proxy get the dirty properties&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;When updating an entity in a different session, the previousState argument would be null!!&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.SetCurrentState(propertyNames, currentState);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.FindDirtyIndexes(propertyNames);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Checks weather the given entity is transient using the proxy.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool? IsTransient(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.IsNew; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// After a flush of an entity, the init state must be called, because the current Current value is actually the new previous value&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void PostFlush(System.Collections.ICollection entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (IStatefullProxy proxy in entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets the entity name&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;Because the entity is actually a proxy, we gotta return the base type of the entity, which is the actual entity type&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override string GetEntityName(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return entity.GetType().BaseType.FullName;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Keeping Entity State Over Multiple NHibernate Sessions</title><link>http://nhforge.org/wikis/howtonh/keeping-entity-state-over-multiple-nhibernate-sessions/revision/1.aspx</link><pubDate>Sat, 11 Sep 2010 08:51:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:345</guid><dc:creator>nadavsof</dc:creator><description>Revision 1 posted to How to by nadavsof on 11/09/2010 05:51:24 a.m.&lt;br /&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p dir="RTL" align="center" class="MsoNormal"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="text-decoration:underline;"&gt;Why would you want to use this source?&lt;/span&gt;&lt;/strong&gt; You&amp;#39;ll want to use
this source if your using NHibernate and need your entities to maintain their
state through long conversations, over multiple NHibernate sessions. You might
also want to use it if you want built in auditing power and general state
oriented methods implemented once and kept through the help of nhibernate.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The problem:&lt;/span&gt;&lt;/strong&gt; NHibernate keeps track of entities within an
NHibernate session (ISession). This means that if you fetch entities within a
session, change those entities and flush the session, the session would know
exactly what you changed, which works very well in most cases &amp;ndash; when it is
possible to re-fetch an entity within any new session, but sometimes this isn&amp;#39;t
enough. Example: multiple views that modifying the same entity instance, when
any one of the views has a &lt;strong&gt;Save&lt;/strong&gt; button, which is the time that the
entity will actually be saved to the DB. In this case, we can&amp;#39;t re-fetch the
entity because it was changed and the DB (or cache) version is irrelevant.
People will tell you &amp;ndash; use a DTO, well, sometimes you gotta use a DTO (when
serializing for example), but here, if we could just modify our previously
fetched entity (it was fetched within an already disposed session), each time
attaching it to the current session (to enable lazy loading), and at the end
saving the changes with nhibernate knowing exactly what was changed (for
dynamic-update for example), wouldn&amp;#39;t it be great?&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The solution - highlevel:&lt;/span&gt;&lt;/strong&gt; Using dynamic proxies that keep
the state of the entities. This way the entities are ignorant of their state
being kept, while the proxies are in charge of keeping the entities&amp;#39; state, and
the NHibernate session interceptor is in charge of using those proxies, to find
out the previous state of an entity, so it is kept through multiple sessions.
The only catch &amp;ndash; each entity, when being instantiated, must be wrapped with a
proxy.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;The usage:&lt;/span&gt;&lt;/strong&gt; 2 main classes concern the End-Developer:&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;1.NHibernateInterceptor&lt;/span&gt; &amp;ndash; Implementor of the &lt;span&gt;NHibernate.&lt;span&gt;IInterceptor &lt;/span&gt;&lt;/span&gt;&lt;span&gt;interface, which &lt;/span&gt;intercept certain events of the session (like &lt;strong&gt;FindDirty).&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;How should it be used?&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;When creating a session, the interceptor should be injected into the
session. The interceptor also gets the session factory as a c&amp;#39;tor argument. The
following code demonstrates its usage:&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;ISession&lt;/span&gt;&lt;span&gt; session =&amp;nbsp;&lt;/span&gt;sessionFactory.OpenSession(&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;NHibernateInterceptor&lt;/span&gt;(sessionFactory))&lt;/span&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;2.StatefullEntityProxyFactory&lt;/span&gt; &amp;ndash; A factory to be used
when instantiating an entity. This class is responsible for creating a proxy
from an existing, or non existing entity instance. The proxy will appear to the
application as if it is the actual entity, with one exception &amp;ndash; the &lt;strong&gt;GetType()&lt;/strong&gt;
method. The proxy, although appears as the actual entity, can also be looked at
as the statefull entity proxy.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;How should it be used?&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;Instantiating an entity with a default 0-params c&amp;#39;tor:&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy&amp;lt;&lt;span&gt;Entity&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&amp;hellip;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&amp;lt;Set values, do stuff&amp;gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;&amp;hellip;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;session.SaveOrUpdate(entity);&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;i&gt;Instantiating a proxy on an existing entity:&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/i&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;Entity&lt;/span&gt;&lt;span&gt; entity = new Entity(1,&amp;quot;name&amp;quot;);&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;entity =&amp;nbsp;&lt;/span&gt;&lt;span&gt;new&lt;/span&gt;&lt;span&gt; &lt;span&gt;StatefullEntityProxyFactory&lt;/span&gt;()&lt;/span&gt;.InstantiateEntityProxy(entity)
as Entity;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;The above two examples are the use cases for instantiating a proxy
instead of an entity, so that its state will be kept. These examples are shown
for creating new entities. When an entity is fetched with NHibernate, the
NHibernateInterceptor will do the same, so that the state will be kept for the
nhibernate fetched entities.&amp;nbsp;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Ok, but why does it work?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li dir="LTR" class="MsoNormal"&gt;The &lt;span&gt;StatefullEntityProxyInterceptor&lt;/span&gt;
     class is a LinFu dynamic proxies interceptor, which holds a reference to a
     &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt; instance and to an entity. It forwards all the
     calls (properties and methods) to the underlying entity, except for a call
     to get the &lt;strong&gt;&lt;i&gt;StateKeeper&lt;/i&gt;&lt;/strong&gt;. This call is possible if we do: &lt;br /&gt;
     
     &lt;/li&gt;
&lt;/ol&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&lt;span&gt;StateKeerp &lt;/span&gt;&lt;span&gt;stateKeeper
= proxy.StateKeeper;&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li dir="LTR" class="MsoNormal"&gt;&lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; &lt;/span&gt;is
     an interface we&amp;#39;re making the proxy implement (using the LinFu proxies
     API), and that defines a property of type &lt;strong&gt;&lt;i&gt;StateKeeper&lt;br /&gt;
     &lt;br /&gt;
     &lt;/i&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li dir="LTR" class="MsoNormal"&gt;The &lt;span&gt;StateKeeper&lt;/span&gt; class has
     a previous state and a current state, and implements a FindDirty method,
     that finds the dirty properties (by comparing the previous and current
     states). The nhibernate interceptor, in its &lt;strong&gt;OnLoad&lt;/strong&gt; event will set
     the previous state of the entity, and in the &lt;strong&gt;PostFlush&lt;/strong&gt; it will call
     the &lt;strong&gt;InitState &lt;/strong&gt;method of the state keeper. In the &lt;strong&gt;FindDirty&lt;/strong&gt;,
     it will of course use the FindDirty method. Example (from the OnLoad
     event): &lt;br /&gt;
     &lt;br /&gt;
     &lt;span&gt;IStatefullProxy&lt;/span&gt;&lt;span&gt; proxy = (&lt;span&gt;IStatefullProxy&lt;/span&gt;)entity;&lt;br /&gt;
     proxy.StateKeeper.InitState(propertyNames, state);&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;To fully understand how it works, it is recommended that you setup a
small application with a couple of entities, map them with NHibernate,
Instantiate them with the above described proxy factory, create the session
injecting the interceptor, put breakpoints in every method of the
NHibernateInterceptor and watch what&amp;#39;s going on when you
save\update\fetch\attach your entities to a different session.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;What else can be done with this idea?&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;Well, lots of stuff. An example of a generic IsModified method is there
&amp;ndash; just get the underlying StateKeeper as shown in the examples above, and call
IsModified. FindDirty, which returns the dirty property names can be useful for
auditing as well, and the idea of IsModified can also be extended to implement
RejectChanges.&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;lt;o:p&amp;gt;&amp;nbsp;&amp;lt;/o:p&amp;gt;&lt;strong&gt;Technologies used: &lt;/strong&gt;NHibernate 3.0 Alpha2, LinFu dynamic proxies.
The source what not tested against NHibernate 2.1, but should work as no NH3 specific
feature is being used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p dir="LTR" class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;The source:&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interface for a statefull proxy - a proxy for some entity that also holds a state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// that manages the state&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public interface IStatefullProxy&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StateKeeper StateKeeper { get; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;namespace StatefullEntityProxies&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Keeps the state of an entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StateKeeper&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StateKeeper(object entity)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The previous state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; previousState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// The current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private IDictionary&amp;lt;string, object&amp;gt; currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Set the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted properties&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The dictionary to populate&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void SetState(string[] properties, object[] values, IDictionary&amp;lt;string, object&amp;gt; state)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; properties.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;state[properties[i]] = values[i];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets weather the entity is a new one&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsNew { get; protected set; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = this.currentState;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.IsNew = false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Init the state of the state keeper with the current state of the entity&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void InitState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetCurrentState(properties, values);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.InitState();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the current state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetCurrentState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.currentState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Sets the previous state of the entity for the keeper&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;properties&amp;quot;&amp;gt;The persisted property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;values&amp;quot;&amp;gt;The corresponding values&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void SetPreviousState(string[] properties, object[] values)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.previousState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.SetState(properties, values, this.previousState);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty property names&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Enumeration of the dirty property names&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IEnumerable&amp;lt;string&amp;gt; FindDirty()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsNew)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new string[0];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.currentState == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;string&amp;gt; dirtyProperties = new List&amp;lt;string&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (KeyValuePair&amp;lt;string, object&amp;gt; property in this.previousState)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object previousValue = this.previousState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object newValue = this.currentState[property.Key];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.IsChanged(previousValue, newValue))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dirtyProperties.Add(property.Key);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return dirtyProperties;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Check weather a specific property value is dirty&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;previousValue&amp;quot;&amp;gt;previous value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;newValue&amp;quot;&amp;gt;new value&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;true if its dirty&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private bool IsChanged(object previousValue, object newValue)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!NHibernateUtil.IsInitialized(previousValue) &amp;amp;&amp;amp; !(NHibernateUtil.IsInitialized(newValue)))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue is AbstractPersistentCollection)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (previousValue as AbstractPersistentCollection).IsDirty;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (previousValue == null &amp;amp;&amp;amp; newValue == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else if (previousValue != null &amp;amp;&amp;amp; newValue != null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return !previousValue.Equals(newValue);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// one of them is null and the other isn&amp;#39;t&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties corresponding indexes&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;propertyNames&amp;quot;&amp;gt;The property names&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The indexes of the dirty properties&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int[] FindDirtyIndexes(string[] propertyNames)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IEnumerable&amp;lt;string&amp;gt; dirtyProperties = this.FindDirty();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties == null)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return null;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;int&amp;gt; indexes = new List&amp;lt;int&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; propertyNames.Length; i++)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (dirtyProperties.Contains(propertyNames[i]))&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;indexes.Add(i);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return indexes.ToArray();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool IsModified&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.ReadStateFromEntity();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.FindDirty().Any())&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return false;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void ReadStateFromEntity()&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState = new Dictionary&amp;lt;string, object&amp;gt;();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (string propertyName in this.previousState.Keys)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MemberInfo member =this.entity.GetType().GetMember(&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;propertyName,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.First();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (member is PropertyInfo)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((PropertyInfo)member).GetValue(this.entity, null);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.currentState[propertyName] = ((FieldInfo)member).GetValue(this.entity);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// A Statefull entity proxy that is constructed from an entity, forwarding all the calls to it&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// and a statekeeper that can be retrieved by the IStatefullProxy interface&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyInterceptor : global::LinFu.DynamicProxy.IInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private StateKeeper stateKeeper;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private object entity;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiates the proxy with a given entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity that its state will be managed by this proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public StatefullEntityProxyInterceptor(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.stateKeeper = new StateKeeper(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.entity = entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Part of the linfu interceptor interface. This method is called for each virtual property or method call on the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;info&amp;quot;&amp;gt;The invocation info on the current call&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;Return the return value of the method/property call&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public object Intercept(LinFu.DynamicProxy.InvocationInfo info)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If its a call to get the state keeper, return the proxy&amp;#39;s underlying state keeper&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (info.TargetMethod.Name == &amp;quot;get_StateKeeper&amp;quot;)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.stateKeeper;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// else, forward the call to the actual entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return info.TargetMethod.Invoke(this.entity, info.Arguments);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int GetHashCode()&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.GetHashCode();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool Equals(object obj)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.entity.Equals(obj);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Factory for generating entity proxies&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class StatefullEntityProxyFactory&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static readonly global::LinFu.DynamicProxy.ProxyFactory factory = &lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new global::LinFu.DynamicProxy.ProxyFactory();&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy from an existing entity instance&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;entity&amp;quot;&amp;gt;The entity to proxy&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxied entity&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Creates a new proxy interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StatefullEntityProxyInterceptor stateKeeperProxy = new StatefullEntityProxyInterceptor(entity);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Generate a proxy, with the statefull entity interceptor as the interceptor, and the entity type as the derrived type&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (IStatefullProxy)factory.CreateProxy(entity.GetType(), stateKeeperProxy, typeof(IStatefullProxy));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;objectType&amp;quot;&amp;gt;Type to create a proxy for&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The generated proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStatefullProxy InstantiateEntityProxy(Type objectType)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object objectToProxy = objectType.GetConstructor(new Type[0]).Invoke(new object[0]);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return this.InstantiateEntityProxy(objectToProxy);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a proxy for the given type using the default empty params c&amp;#39;tor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;typeparam name=&amp;quot;TEntity&amp;quot;&amp;gt;The type to craete proxy for&amp;lt;/typeparam&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;returns&amp;gt;The proxy&amp;lt;/returns&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public TEntity InstantiateEntityProxy&amp;lt;TEntity&amp;gt;() where TEntity : class&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (TEntity)(this.InstantiateEntityProxy(typeof(TEntity)));&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;namespace StatefullEntityProxies&lt;/div&gt;
&lt;div&gt;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// Interceptor that intercept events of the session that it is binds to, and assumes that all entities are actually&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// statefull entity proxies, and uses the underlying state keeper to determine the previous state of entities&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// so that the state is kepts even through different sessions&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;h4&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class NHibernateInterceptor : EmptyInterceptor&lt;/h4&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new interceptor&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;param name=&amp;quot;sessionFactory&amp;quot;&amp;gt;The session factory that is used to generate the current session&amp;lt;/param&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public NHibernateInterceptor(ISessionFactory sessionFactory)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.Factory = sessionFactory as ISessionFactoryImplementor;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private ISessionFactoryImplementor Factory {get;set;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Creates a new entity instance using the statefull entity proxy factory&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;The call to this method is made by nhibernate when loading entities from queries&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override object Instantiate(string clazz, EntityMode entityMode, object id)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Get the type of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Type type = Type.GetType(clazz);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a new instance of the entity using the persister of the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object entity = this.Factory.GetEntityPersister(clazz).Instantiate(id, entityMode);&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a proxy from the entity&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object proxy = new StatefullEntityProxyFactory().InstantiateEntityProxy(entity);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Initiate the state of the proxy when an entity is loaded&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;This happens after the instantiation of an entity (a proxy)&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState(propertyNames, state);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return base.OnLoad(entity, id, state, propertyNames, types);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Finds the dirty properties of the entity. Uses the proxy get the dirty properties&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;When updating an entity in a different session, the previousState argument would be null!!&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.SetCurrentState(propertyNames, currentState);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.FindDirtyIndexes(propertyNames);&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Checks weather the given entity is transient using the proxy.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override bool? IsTransient(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStatefullProxy proxy = (IStatefullProxy)entity;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return proxy.StateKeeper.IsNew; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// After a flush of an entity, the init state must be called, because the current Current value is actually the new previous value&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void PostFlush(System.Collections.ICollection entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;foreach (IStatefullProxy proxy in entities)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;proxy.StateKeeper.InitState();&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Gets the entity name&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;/summary&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// &amp;lt;remarks&amp;gt;Because the entity is actually a proxy, we gotta return the base type of the entity, which is the actual entity type&amp;lt;/remarks&amp;gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override string GetEntityName(object entity)&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return entity.GetType().BaseType.FullName;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/p&gt;</description></item><item><title>NHibernate Features</title><link>http://nhforge.org/wikis/howtonh/nhibernate-features/revision/0.aspx</link><pubDate>Sun, 18 Jul 2010 15:08:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:78</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 18/07/2010 12:08:54 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Features&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Architecture&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Configuration&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Documentation&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;Nearly all of the features listed here are described more fully in the document:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate - Relational Persistence for Idiomatic .NET&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;a href="/doc/nh/en/index.html"&gt;http://nhforge.org/doc/nh/en/index.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Numerous databases supported
&lt;ul&gt;
&lt;li&gt;SQL Server (2000, 2005, 2008, Express&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CE&lt;/span&gt;)&lt;/li&gt;
&lt;li&gt;Oracle (9i, 10g, 11g)&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;DB2&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Firebird&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Informix&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;MySQL&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PostgreSQL&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SQLite&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Sybase&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Flexible mapping strategies&lt;/li&gt;
&lt;li&gt;Based on Hibernate (Java)
&lt;ul&gt;
&lt;li&gt;Started as a port of Hibernate&lt;/li&gt;
&lt;li&gt;Includes nearly all features of current Hibernate&lt;/li&gt;
&lt;li&gt;Extends Hibernate by adding LINQ capabilities&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Continuous integration development environment&lt;/li&gt;
&lt;li&gt;Flexible query APIs&lt;/li&gt;
&lt;li&gt;Multiple session configuration models&lt;/li&gt;
&lt;li&gt;Logging support&lt;/li&gt;
&lt;li&gt;Support for audit integration&lt;/li&gt;
&lt;li&gt;Works with stored procedures&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Works&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; triggers&lt;/li&gt;
&lt;li&gt;Significant body of add ons &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;a href="/wikis/general/open-source-project-ecosystem.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Open&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Source&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;a href="/wikis/general/commercial-product-ecosystem.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Commercial&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Multiple cache choices&lt;/li&gt;
&lt;li&gt;Outer-join fetching, when supported by database&lt;/li&gt;
&lt;li&gt;Batch SQL, when supported by database&lt;/li&gt;
&lt;li&gt;Flexible connection management&lt;/li&gt;
&lt;li&gt;Query language substitutions&lt;/li&gt;
&lt;li&gt;Internal naming strategy definition&lt;/li&gt;
&lt;li&gt;Support for dynamic model&lt;/li&gt;
&lt;li&gt;Multiple proxy implementation choices&lt;/li&gt;
&lt;li&gt;Lazy fetching for associations&lt;/li&gt;
&lt;li&gt;Dynamic insert and update generation&lt;/li&gt;
&lt;li&gt;Custom persistence mechanisms&lt;/li&gt;
&lt;li&gt;Multiple Id generators&lt;/li&gt;
&lt;li&gt;Support for Composite Keys&lt;/li&gt;
&lt;li&gt;Multiple property access strategies&lt;/li&gt;
&lt;li&gt;Flexible association mapping&lt;/li&gt;
&lt;li&gt;Component mapping&lt;/li&gt;
&lt;li&gt;Ability to map an association from multiple collection types&lt;/li&gt;
&lt;li&gt;Custom value types&lt;/li&gt;
&lt;li&gt;Supports bidirectional associations&lt;/li&gt;
&lt;li&gt;Inheritance mapping&lt;/li&gt;
&lt;li&gt;Metadata API&lt;/li&gt;
&lt;li&gt;Stateless session capability&lt;/li&gt;
&lt;li&gt;Direct DML capability&lt;/li&gt;
&lt;li&gt;DDL schema generation from mapping files&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate Features</title><link>http://nhforge.org/wikis/howtonh/nhibernate-features/revision/2.aspx</link><pubDate>Mon, 21 Jun 2010 23:19:39 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:330</guid><dc:creator>John Davidson</dc:creator><description>Revision 2 posted to How to by John Davidson on 21/06/2010 08:19:39 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate Features&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Architecture, Configuration, Nhibernate Documentation&lt;/div&gt;

&lt;p&gt;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Features&lt;/span&gt;&lt;p&gt;Nearly all of the features listed here are described more fully in the document:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate - Relational Persistence for Idiomatic .NET&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;a href="/doc/nh/en/index.html"&gt;http://nhforge.org/doc/nh/en/index.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Numerous databases supported
&lt;ul&gt;
&lt;li&gt;SQL Server (2000, 2005, 2008, Express)&lt;/li&gt;
&lt;li&gt;Oracle (9i, 10g, 11g)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Flexible mapping strategies&lt;/li&gt;
&lt;li&gt;Based on Hibernate (Java)
&lt;ul&gt;
&lt;li&gt;Started as a port of Hibernate&lt;/li&gt;
&lt;li&gt;Includes nearly all features of current Hibernate&lt;/li&gt;
&lt;li&gt;Extends Hibernate by adding LINQ capabilities&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Continuous integration development environment&lt;/li&gt;
&lt;li&gt;Flexible query APIs&lt;/li&gt;
&lt;li&gt;Multiple session configuration models&lt;/li&gt;
&lt;li&gt;Logging support&lt;/li&gt;
&lt;li&gt;Support for audit integration&lt;/li&gt;
&lt;li&gt;Works with stored procedures&lt;/li&gt;
&lt;li&gt;Works with triggers&lt;/li&gt;
&lt;li&gt;Significant body of add ons&lt;/li&gt;
&lt;li&gt;Multiple cache choices&lt;/li&gt;
&lt;li&gt;Outer-join fetching, when supported by database&lt;/li&gt;
&lt;li&gt;Batch SQL, when supported by database&lt;/li&gt;
&lt;li&gt;Flexible connection management&lt;/li&gt;
&lt;li&gt;Query language substitutions&lt;/li&gt;
&lt;li&gt;Internal naming strategy definition&lt;/li&gt;
&lt;li&gt;Support for dynamic model&lt;/li&gt;
&lt;li&gt;Multiple proxy implementation choices&lt;/li&gt;
&lt;li&gt;Lazy fetching for associations&lt;/li&gt;
&lt;li&gt;Dynamic insert and update generation&lt;/li&gt;
&lt;li&gt;Custom persistence mechanisms&lt;/li&gt;
&lt;li&gt;Multiple Id generators&lt;/li&gt;
&lt;li&gt;Support for Composite Keys&lt;/li&gt;
&lt;li&gt;Multiple property access strategies&lt;/li&gt;
&lt;li&gt;Flexible association mapping&lt;/li&gt;
&lt;li&gt;Component mapping&lt;/li&gt;
&lt;li&gt;Ability to map an association from multiple collection types&lt;/li&gt;
&lt;li&gt;Custom value types&lt;/li&gt;
&lt;li&gt;Supports bidirectional associations&lt;/li&gt;
&lt;li&gt;Inheritance mapping&lt;/li&gt;
&lt;li&gt;Metadata API&lt;/li&gt;
&lt;li&gt;Stateless session capability&lt;/li&gt;
&lt;li&gt;Direct DML capability&lt;/li&gt;
&lt;li&gt;DDL schema generation from mapping files&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate Features</title><link>http://nhforge.org/wikis/howtonh/nhibernate-features/revision/1.aspx</link><pubDate>Mon, 21 Jun 2010 23:17:21 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:324</guid><dc:creator>John Davidson</dc:creator><description>Revision 1 posted to How to by John Davidson on 21/06/2010 08:17:21 p.m.&lt;br /&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate Features&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Nearly all of the features listed here are described more fully in the document: &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate - Relational Persistence for Idiomatic .NET&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;a href="/doc/nh/en/index.html"&gt;http://nhforge.org/doc/nh/en/index.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Numerous databases supported&lt;/li&gt;
&lt;li&gt;SQL Server (2000, 2005, 2008, Express)&lt;/li&gt;
&lt;li&gt;Oracle (9i, 10g, 11g)&lt;/li&gt;
&lt;li&gt;Flexible mapping strategies&lt;/li&gt;
&lt;li&gt;Based on Hibernate (Java)&lt;/li&gt;
&lt;li&gt;Started as a port of Hibernate&lt;/li&gt;
&lt;li&gt;Includes nearly all features of current Hibernate&lt;/li&gt;
&lt;li&gt;Extends Hibernate by adding LINQ capabilities&lt;/li&gt;
&lt;li&gt;Continuous integration development environment&lt;/li&gt;
&lt;li&gt;Flexible query APIs&lt;/li&gt;
&lt;li&gt;Multiple session configuration models&lt;/li&gt;
&lt;li&gt;Logging support&lt;/li&gt;
&lt;li&gt;Support for audit integration&lt;/li&gt;
&lt;li&gt;Works with stored procedures&lt;/li&gt;
&lt;li&gt;Works with triggers&lt;/li&gt;
&lt;li&gt;Significant body of add ons&lt;/li&gt;
&lt;li&gt;Multiple cache choices&lt;/li&gt;
&lt;li&gt;Outer-join fetching, when supported by database&lt;/li&gt;
&lt;li&gt;Batch SQL, when supported by database&lt;/li&gt;
&lt;li&gt;Flexible connection management&lt;/li&gt;
&lt;li&gt;Query language substitutions&lt;/li&gt;
&lt;li&gt;Internal naming strategy definition&lt;/li&gt;
&lt;li&gt;Support for dynamic model&lt;/li&gt;
&lt;li&gt;Multiple proxy implementation choices&lt;/li&gt;
&lt;li&gt;Lazy fetching for associations&lt;/li&gt;
&lt;li&gt;Dynamic insert and update generation&lt;/li&gt;
&lt;li&gt;Custom persistence mechanisms&lt;/li&gt;
&lt;li&gt;Multiple Id generators&lt;/li&gt;
&lt;li&gt;Support for Composite Keys&lt;/li&gt;
&lt;li&gt;Multiple property access strategies&lt;/li&gt;
&lt;li&gt;Flexible association mapping&lt;/li&gt;
&lt;li&gt;Component mapping&lt;/li&gt;
&lt;li&gt;Ability to map an association from multiple collection types&lt;/li&gt;
&lt;li&gt;Custom value types&lt;/li&gt;
&lt;li&gt;Supports bidirectional associations&lt;/li&gt;
&lt;li&gt;Inheritance mapping&lt;/li&gt;
&lt;li&gt;Metadata API&lt;/li&gt;
&lt;li&gt;Stateless session capability&lt;/li&gt;
&lt;li&gt;Direct DML capability&lt;/li&gt;
&lt;li&gt;DDL schema generation from mapping files&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>How To Get Help with NHibernate</title><link>http://nhforge.org/wikis/howtonh/how-to-get-help-with-nhibernate/revision/0.aspx</link><pubDate>Sat, 12 Jun 2010 23:36:51 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:77</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 12/06/2010 08:36:51 p.m.&lt;br /&gt;
&lt;h2&gt;How To Get Help with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;The best way to get help with NHibernate is to ask a question on the &lt;a href="http://groups.google.com/group/nhusers)"&gt;NHusers Group&lt;/a&gt;. When asking a questing remember to provide enough detail so that those who wish to help you can analyze your request when you first make it without having to ask for further details. The various mapping files, query definitions and sometimes configuration or program details should be provided when they are relevant to the discussion.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Google Groups&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers)"&gt;International (English language)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/NHibernate-Hispanol"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Espanol&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhibernate-fr"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Francais&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/NHibernate-Hispanol"&gt;&lt;span style="background: SpringGreen;"&gt;Espa&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;ntilde;ol&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhibernate-fr"&gt;&lt;span style="background: SpringGreen;"&gt;Fran&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;ccedil;ais&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nh-it"&gt;Italiano&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nhibernate-br"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Portugues&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nhibernate-br"&gt;&lt;span style="background: SpringGreen;"&gt;Portugu&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;ecirc;s&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://home.cnblogs.com/group/NHibernate/"&gt;Chinese&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Other Sources&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This site has a search function that may return a relevant answer, from the many Wiki or Blog entries posted here. A Google search will lead to many other blogs with helpful information.&lt;/p&gt;
&lt;p&gt;There are 3 books written about NHibernate. They can be seen&amp;nbsp;&lt;a href="/content/Books.aspx"&gt;here&lt;/a&gt;&amp;nbsp;with links to buy them. The first 2 were written with NHibernate version 1.2, but still have relevant information for even the latest version, though the feature list has expanded greatly. The 3rd book was written base on version 2.1 and was just released in May 2010.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>How To Get Help with NHibernate</title><link>http://nhforge.org/wikis/howtonh/how-to-get-help-with-nhibernate/revision/3.aspx</link><pubDate>Sat, 12 Jun 2010 23:31:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:323</guid><dc:creator>John Davidson</dc:creator><description>Revision 3 posted to How to by John Davidson on 12/06/2010 08:31:15 p.m.&lt;br /&gt;
&lt;h2&gt;How To Get Help with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate&lt;/div&gt;

&lt;p&gt;The best way to get help with NHibernate is to ask a question on the &lt;a href="http://groups.google.com/group/nhusers)"&gt;NHusers Group&lt;/a&gt;. When asking a questing remember to provide enough detail so that those who wish to help you can analyze your request when you first make it without having to ask for further details. The various mapping files, query definitions and sometimes configuration or program details should be provided when they are relevant to the discussion.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Google Groups&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers)"&gt;International (English language)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/NHibernate-Hispanol"&gt;Espanol&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhibernate-fr"&gt;Francais&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nh-it"&gt;Italiano&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nhibernate-br"&gt;Portugues&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://home.cnblogs.com/group/NHibernate/"&gt;Chinese&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Other Sources&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This site has a search function that may return a relevant answer, from the many Wiki or Blog entries posted here. A Google search will lead to many other blogs with helpful information.&lt;/p&gt;
&lt;p&gt;There are 3 books written about NHibernate. &lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;They&lt;/span&gt; can be seen&lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &amp;nbsp;&lt;a href="/content/Books.aspx"&gt;here&lt;/a&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;links&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;them&lt;/span&gt;. The first 2 were written with NHibernate version 1.2, but still have relevant information for even the latest version, though the feature list has expanded greatly. The 3rd book was written base on version 2.1 and was just released in May 2010.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>How To Get Help with NHibernate</title><link>http://nhforge.org/wikis/howtonh/how-to-get-help-with-nhibernate/revision/2.aspx</link><pubDate>Sat, 12 Jun 2010 23:30:00 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:322</guid><dc:creator>John Davidson</dc:creator><description>Revision 2 posted to How to by John Davidson on 12/06/2010 08:30:00 p.m.&lt;br /&gt;
&lt;h2&gt;How To Get Help with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Nhibernate&lt;/div&gt;

&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;best&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;way&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;help&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ask&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;question&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;a href="http://groups.google.com/group/nhusers)"&gt;&lt;span style="background: SpringGreen;"&gt;NHusers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Group&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;When&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;asking&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;questing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;remember&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;enough&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;detail&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;so&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;those&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;who&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;wish&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;help&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;analyze&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;request&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;without&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;having&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ask&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;further&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;details&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;various&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;files&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;definitions&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sometimes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;program&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;details&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provided&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;relevant&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;discussion&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Google Groups&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers)"&gt;International (English language)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/NHibernate-Hispanol"&gt;Espanol&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhibernate-fr"&gt;Francais&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nh-it"&gt;Italiano&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nhibernate-br"&gt;Portugues&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://home.cnblogs.com/group/NHibernate/"&gt;Chinese&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Other&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Sources&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;site&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;search&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;function&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;may&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;relevant&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;answer&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;many&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Wiki&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entries&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;posted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;A&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Google&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;search&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;will&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lead&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;many&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;other&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blogs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;helpful&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;books&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;written&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;seen&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;a href="/content/Books.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;first&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;2&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;were&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;written&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;1.2&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;still&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;relevant&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;information&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;even&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;latest&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;though&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;feature&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expanded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;greatly&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3rd&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;book&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;written&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;base&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;2.1&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;released&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;May&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;2010&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>How To Get Help with NHibernate</title><link>http://nhforge.org/wikis/howtonh/how-to-get-help-with-nhibernate/revision/1.aspx</link><pubDate>Sat, 12 Jun 2010 23:02:07 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:321</guid><dc:creator>John Davidson</dc:creator><description>Revision 1 posted to How to by John Davidson on 12/06/2010 08:02:07 p.m.&lt;br /&gt;
&lt;p&gt;&lt;strong&gt;Google Groups&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers)"&gt;International (English language)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/NHibernate-Hispanol"&gt;Espanol&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhibernate-fr"&gt;Francais&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nh-it"&gt;Italiano&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com.ar/group/nhibernate-br"&gt;Portugues&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://home.cnblogs.com/group/NHibernate/"&gt;Chinese&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Calling Oracle Procedure inside package</title><link>http://nhforge.org/wikis/howtonh/calling-oracle-procedure-inside-package/revision/0.aspx</link><pubDate>Wed, 09 Jun 2010 19:04:59 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:76</guid><dc:creator>Paul V.</dc:creator><description>Current revision posted to How to by Paul V. on 09/06/2010 04:04:59 p.m.&lt;br /&gt;
&lt;h2&gt;Calling &lt;span style="text-decoration: line-through; color: red;"&gt;Orcale&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Oracle&lt;/span&gt; Procedure inside package&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Conversation&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I want to call oracle stored procedure in package.I used like this returns error.&lt;/p&gt;
&lt;p&gt;Call PKG.GET_EMPLOYEES()&lt;/p&gt;
&lt;p&gt;Then i remove the procedure from package and defined outside the package as global procedure .Then it works&lt;/p&gt;
&lt;p&gt;Call GET_EMPLOYEES()&lt;/p&gt;
&lt;p&gt;So how to call procedure with package name. Please help.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Calling Orcale Procedure inside package</title><link>http://nhforge.org/wikis/howtonh/calling-oracle-procedure-inside-package/revision/1.aspx</link><pubDate>Wed, 09 Jun 2010 13:59:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:319</guid><dc:creator>suresh.rajamani</dc:creator><description>Revision 1 posted to How to by suresh.rajamani on 09/06/2010 10:59:15 a.m.&lt;br /&gt;
&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I want to call oracle stored procedure in package.I used like this returns error.&lt;/p&gt;
&lt;p&gt;Call PKG.GET_EMPLOYEES()&lt;/p&gt;
&lt;p&gt;Then i remove the procedure from package and defined outside the package as global procedure .Then it works&lt;/p&gt;
&lt;p&gt;Call GET_EMPLOYEES()&lt;/p&gt;
&lt;p&gt;So how to call procedure with package name. Please help.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>How to use Db2hbm</title><link>http://nhforge.org/wikis/howtonh/how-to-use-db2hbm/revision/0.aspx</link><pubDate>Fri, 04 Jun 2010 13:38:09 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:72</guid><dc:creator>supersi</dc:creator><description>Current revision posted to How to by supersi on 04/06/2010 10:38:09 a.m.&lt;br /&gt;
&lt;h2&gt;How to use Db2hbm&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;/div&gt;

&lt;p align="justify"&gt;Db2hbm is a reverse engineering tool able to generate hbm files from a database schema and some information contained in a configuration file. Unlike other commercial and open source tools serving the same purpose, db2hbm is not template based. The hbm output comes from a serialization based on the NHibernate mapping schema classes. Extendibility is provided through&amp;nbsp; strategies the user can implement or extend. db2hbm does not generate code. In order to generate the classes or other artifacts please consider the use of &lt;a href="/blogs/nhibernate/archive/2009/12/12/t4-hbm2net-alpha-2.aspx"&gt;hbm2net&lt;/a&gt;. &lt;/p&gt;
&lt;p align="justify"&gt;Db2hbm is a command line tool. &lt;a target="_blank" href="/media/p/615.aspx"&gt;You can download it here&lt;/a&gt;. Information on how to connect to the database and various generation options are contained in a separate configuration file you have to provide in order to make the tool work. A schema for the configuration file is provided in the distributed zip:&lt;b&gt;nhibernate-codegen.xsd &lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;a href="https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Tool.hbm2net/src/NHibernate.Tool.Db2hbm/"&gt;&lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;download&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;b&gt;.&lt;/b&gt; You will have intellisense editing the configuration file in Visual Studio by copying that file under the&lt;b&gt; %Program Files%\Microsoft Visual Studio 9.0\Xml\Schemas&lt;/b&gt; folder. Below there is a configuration file skeleton and a description for each section is provided.&lt;/p&gt;
&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-family:consolas;background:yellow;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:yellow;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;xml&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;version&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;encoding&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:yellow;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:yellow;"&gt;?&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;db2hbm-conf&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;xmlns&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;urn:nhibernate-codegen-2.2&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;metadata-strategies&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.FirstPassEntityCollector, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.PrimaryKeyStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.ManyToOneStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.SetStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.ManyToManyStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.ApplyEntityExceptionsStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:green;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;!--strategy class=&amp;quot;NHibernate.Tool.Db2hbm.SetToMapStrategy, NHibernate.Tool.Db2hbm&amp;quot;/--&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/metadata-strategies&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;foreign-key-crawlers&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;factory&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;NHibernate.Tool.Db2hbm.ForeignKeyCrawlers.MSSQLForeignKeyCrawlerFactory, NHibernate.Tool.Db2hbm&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/factory&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/foreign-key-crawlers&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;type-mapping&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;xml&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:green;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;!--user type instead--&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;bit&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Boolean&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;varbinary&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Array&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;varchar&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;nvarchar&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;nchar&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;char&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;numeric&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;tinyint&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Byte&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;smallint&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Int16&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;int&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Int32&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;bigint&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Int64&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;datetime&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;DateTime&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;uniqueidentifier&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Guid&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;money&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;smallmoney&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/type-mapping&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;naming-strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.DefaultNamingStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;property&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Language&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;English&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/naming-strategy&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;connection-info&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;dialect&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;NHibernate.Dialect.MsSql2005Dialect, NHibernate&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/dialect&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;connection-string&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;Server=localhost\SQLEXPRESS;initial catalog=AdventureWorks;Integrated Security=True&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/connection-string&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;connection-driver&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;NHibernate.Driver.SqlClientDriver, NHibernate&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/connection-driver&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/connection-info&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;table-filter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;include&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:green;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;!--Include all--&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;/table-filter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;tables&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;/tables&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;entity-exceptions&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entity&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;.*Addres.*|UnitMeasure|CreditCard&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;member-tag&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;set|bag|idbag|list&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;exclude&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;set&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;lazy&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;value&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;lt;meta-add&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;attribute&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;quot;comment&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;this is a sample meta&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;lt;/meta-add&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entity&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entity&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;.*&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;member-tag&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;set|bag|idbag|list&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;set&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;access&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;value&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;field.camelcase-underscore&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;meta-add&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;attribute&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;test&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;Value of test&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/meta-add&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/member-tag&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;member-tag&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;many-to-one&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;set&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;fetch&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;value&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;join&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/member-tag&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entity&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entity-exceptions&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entities-namespace&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;AdventureWorks.Entities&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entities-namespace&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entities-assembly&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;AdventureWorks.Entities&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entities-assembly&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height:115%;font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/db2hbm-conf&amp;gt;&lt;/span&gt;&lt;span style="line-height:115%;font-size:8pt;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height:115%;font-size:8pt;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;&amp;lt;metadata-strategies&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;This section configure the strategy used to infer the mapping from the schema or from what is inferred from the previous strategies. The execution order is preserved and reflect the order on the configuration file. The current version has the following strategy embedded:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;FirstPassEntityCollector&lt;/b&gt; &amp;ndash; Collect each table in the schema and provide an entity for each table with a property for each column. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;PrimaryKeyStrategy&lt;/b&gt; &amp;ndash; Find the primary keys on the tables and modify the previously collected entities to reflect the proper &amp;lt;id&amp;gt; or &amp;lt;composite-id&amp;gt; declared in the schema. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;ManyToOneStrategy&lt;/b&gt; &amp;ndash; Find the defined foreign keys, and if they point to mapped tables, creates the &amp;lt;many-to-one&amp;gt; associations &lt;/li&gt;
&lt;li&gt;&lt;b&gt;SetStrategy&lt;/b&gt; &amp;ndash; Make collections at the other end points of a &amp;lt;many-to-one&amp;gt; association. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;ManyToManyStrategy&lt;/b&gt; &amp;ndash; Try to infer if there is a link table between two entities. If so the associative entity is dropped and a pair of collection of type &amp;lt;bag&amp;gt; or &amp;lt;idbag&amp;gt; is created to represent the many-to-many association. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;ApplyEntityExceptionsStrategy&lt;/b&gt; &amp;ndash; Applies code generation exceptions to the entities according to the &lt;b&gt;&lt;i&gt;&amp;lt;entity-exceptions&amp;gt;&lt;/i&gt;&lt;/b&gt; configuration section( see below ) &lt;/li&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;Each strategy element, if required, can contain one of more element &amp;lt;property&amp;gt; in the followinf form:&lt;/p&gt;
&lt;p align="justify"&gt;&amp;lt;property name=&amp;quot;X&amp;quot;&amp;gt;yyyyyyy&amp;lt;/property&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;This means: set the value of the property named &amp;ldquo;X&amp;rdquo; to the value yyyyyy. Internally the value is converted to the target type of the property if it is not a string.&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;&amp;lt;foreign-key-crawlers&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;The core strategies used in reverse engineering requires to know how the foreign key are declared on the tables. Since the ADO.NET schema provider model does not fit this requirement, we have to provide a custom strategy for each DB dialect we are using. &lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Currently only MSSQL server is supported&lt;/span&gt;&lt;/b&gt;, so if you want a proper code generation for another database you should provide your own foreign-key.-crawler, or alternatively configure each FK definition by hand, as shown in the &lt;b&gt;&amp;lt;tables&amp;gt;&lt;/b&gt; configuration section ( you would probably prefer not to ).&lt;/p&gt;
&lt;p align="justify"&gt;To provide your own key crawler, you have to create a factory for it, implementing the interface &lt;b&gt;IForeignKeyCrawlerFactory&lt;/b&gt;, declared as shown below:&lt;/p&gt;
&lt;p&gt;public interface IForeignKeyCrawlerFactory    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IForeignKeyCrawler Create();     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void Register();     &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The register method is called by the infrastructure, and allow you to bind your factory with one or more NHIbernate Dialect. Let&amp;rsquo;s have an example on how the MSSQLForeignKeyCrawlerFactory implement this function:&lt;/p&gt;
&lt;p&gt;public void Register()    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var instance = new MSSQLForeignKeyCrawlerFactory();     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForeignKeyCrawlersRegistar.Register(instance, typeof(MsSql2000Dialect));     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForeignKeyCrawlersRegistar.Register(instance, typeof(MsSql2005Dialect));     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForeignKeyCrawlersRegistar.Register(instance, typeof(MsSql2008Dialect));     &lt;br /&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;The&amp;nbsp; code above register an instance of MSSQLForeignKeyCrawlerFactory bound to the MsSql200x dialects.&lt;/p&gt;
&lt;p align="justify"&gt;The other method, Create, are supposed to return the IForeignCrawler instance able to deal with the specific database foreign key schema info. This object must implement the following interface:&lt;/p&gt;
&lt;p&gt;public interface IForeignKeyCrawler {    &lt;br /&gt;IForeignKeyColumnInfo[] GetForeignKeyColumns(DbConnection dbConnection     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , string constraintName     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , string catalog     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , string schema     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; );     &lt;br /&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;To proper implement this you should be able to return an array of &lt;b&gt;IForeignKeyColumnInfo&lt;/b&gt; from the constraint name, the catalog and the schema containing it.&lt;/p&gt;
&lt;p align="justify"&gt;The I&lt;b&gt;ForeignKeyColumnInfo&lt;/b&gt; interface is defined as follow:&lt;/p&gt;
&lt;p&gt;public interface IForeignKeyColumnInfo    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyColumnName { get; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyTableName { get; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyTableSchema { get;&amp;nbsp; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyTableCatalog { get;&amp;nbsp; } &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyColumnName { get; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyTableName { get; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyTableSchema { get;&amp;nbsp; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyTableCatalog { get;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;&lt;b&gt;PrimaryKey&lt;/b&gt;/(Table/Catalog/Column)&lt;b&gt;Name&lt;/b&gt; must return the key column information as defined in the table containing the primary key referenced by the constraint.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;b&gt;ForeignKey&lt;/b&gt;/(Table/Catalog/Column)&lt;b&gt;Name&lt;/b&gt; must return the key column information as defined in the table containing the foreign key referenced by the constraint.&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;&amp;lt;type-mapping&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Each element in this section maps a database type to an NHIbernate type. You can force a type with respect to column length/precision too. So you can have:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&amp;lt;sql-type dbtype=&amp;quot;varchar&amp;quot; length=&amp;quot;1&amp;quot; nhtype=&amp;quot;Char&amp;quot;/&amp;gt;      &lt;br /&gt;&amp;lt;sql-type dbtype=&amp;quot;varchar&amp;quot; length=&amp;quot;2-*&amp;quot; nhtype=&amp;quot;String&amp;quot;/&amp;gt;&lt;/b&gt;&lt;/p&gt;
&lt;p align="justify"&gt;in order to distinguish between char and strings. The most restrictive satisfied condition is used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;naming-strategy&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;The naming strategy is a component driving the name generation for the entities, properties, collections and component key class generated by the tool. A default naming strategy is provided: &lt;b&gt;NHibernate.Tool.Db2hbm.DefaultNamingStrategy&lt;/b&gt;. This strategy leverages the unNHAddins inflector to singularize the name of the tables, or pluralize the collections. The unNHaddins inflector is provided for many languages, the DefaultNamingStrategy infer the concrete class to use from the property language:&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Language&amp;quot;&amp;gt;English&amp;lt;/property&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;If an unknown language is specified, DefaultNamingStrategy uses &amp;ldquo;English &amp;ldquo; by default. In order to implement your own naming strategy, you have to derive from the default naming strategy, or implement your own from scratch by implementing the &lt;b&gt;INamingStrategy interface&lt;/b&gt;. Please note that despites the name, this interface &lt;span style="text-decoration:underline;"&gt;is not the one contained in NHibernate&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The interface is defined as follow:&lt;/p&gt;
&lt;p&gt;public interface INamingStrategy    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetEntityNameFromTableName(string tableName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetPropertyNameFromColumnName(string columnName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetIdPropertyNameFromColumnName(string columnName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetClassNameForComponentKey(string entityName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetNameForComponentKey(string entityName,string componentClass);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetNameForManyToOne(string referredEntity, string[] columnNames);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetNameForCollection(string collectingClass, int progressive);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetClassNameForCollectionComponent(string collectionTableName);     &lt;br /&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;The meaning of the first three functions should be clear by the name. For the other ones:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetClassNameForComponentKey&lt;/b&gt; &amp;ndash; The entity has a composite key, and it is reverse engineering with a component, implementor has to return a name for the class representing that component. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetNameForManyToOne&lt;/b&gt; &amp;ndash; The entity has a reference to another entity, you have to return the name you want to assign to the corresponding property. Implementor will receive the referred entity name and the column(s) names defining the reference. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetNameForCollection&lt;/b&gt; &amp;ndash; the entity has a collection of other entity instances. The implementor will receive the name of the collected entity, plus a progressive ( you may have more collection of the same type of entity in a class ). implementor has to return the name to assign to the property representhing the collection. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetClassNameForCollectionComponent&lt;/b&gt; &amp;ndash; There is a map of component. implementor has to return the name of the class representing the component. &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;connection-info&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Db2hbm needs to connect to the database to inquire into schema information. In this section user will provide the essential parameters to achieve the connection.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;&amp;lt;dialect&amp;gt;&lt;/b&gt; &amp;ndash; the NHibernate dialect class. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;&amp;lt;connection-string&amp;gt;&lt;/b&gt; &amp;ndash; the DB connection string &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;&amp;lt;connection-driver&amp;gt;&lt;/b&gt; &amp;ndash; the NHibernate connection driver &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;table-filter&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;User can specify which table to include/exclude from the reverse engineering properties by adding keys to this section.&lt;/p&gt;
&lt;p align="justify"&gt;More than one include/exclude element can be specified. Each element has three attributes: &lt;/p&gt;
&lt;p align="justify"&gt;&amp;lt;include catalog=&amp;quot;.*&amp;quot; schema=&amp;quot;.*&amp;quot; table=&amp;quot;.*&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;&amp;lt;exclude table=&amp;quot;UselessTable&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;you can specify a regular expression to specify the pattern for the catalog, the schema and the table. Tables matching this pattern will be excluded/included.&lt;/p&gt;
&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;tables&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;If there is not enougth information in the schema, or if there is not a proper foreign key crawler available for the database in use, it is possible to specify manually the foreign key/primary key layout. These information will merge with the ones already present in the schema, if any.&lt;/p&gt;
&lt;p&gt;Here below an example:&lt;/p&gt;
&lt;p&gt;&amp;lt;tables&amp;gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;table schema=&amp;quot;dbo&amp;quot; catalog=&amp;quot;db2hbm&amp;quot; name=&amp;quot;Simple&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;primary-key &amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;sequence&amp;quot; &amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name=&amp;quot;sequence&amp;quot;&amp;gt;id_seq&amp;lt;/param&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/generator&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/primary-key&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;foreign-key constraint-name=&amp;quot;myconstraint&amp;quot; foreign-catalog=&amp;quot;adatabase&amp;quot; foreign-schema=&amp;quot;dbo&amp;quot; foreign-table=&amp;quot;ReferredTable&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;column-ref foreign-column=&amp;quot;id&amp;quot; local-column=&amp;quot;referred-id&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/foreign-key&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/table&amp;gt;     &lt;br /&gt;&amp;nbsp; &amp;lt;/tables&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;entity-exceptions&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;In this section the user can finely drive the produced hbm artifacts by altering the default tool behavior on a per-name o per-tag based strategy. Let&amp;rsquo;s have an example:&lt;/p&gt;
&lt;p&gt;&amp;lt;entity-exceptions&amp;gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;entity match=&amp;quot;.*Addres.*|UnitMeasure|CreditCard&amp;quot;&amp;nbsp; &amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;member-tag match=&amp;quot;set|bag|idbag|list&amp;quot; exclude=&amp;quot;true&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;set name=&amp;quot;lazy&amp;quot; value=&amp;quot;false&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;meta-add attribute=&amp;quot;comment&amp;quot;&amp;gt;a comment&amp;hellip;&amp;lt;/meta-add&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/entity&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;entity match=&amp;quot;.*&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;member-tag match=&amp;quot;set|bag|idbag|list&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;set name=&amp;quot;access&amp;quot; value=&amp;quot;field.camelcase-underscore&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;meta-add attribute=&amp;quot;test&amp;quot;&amp;gt;Value of test&amp;lt;/meta-add&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/member-tag&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;member-tag match=&amp;quot;many-to-one&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;set name=&amp;quot;fetch&amp;quot; value=&amp;quot;join&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/member-tag&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/entity&amp;gt;     &lt;br /&gt;&amp;lt;/entity-exceptions&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p align="justify"&gt;The tag &lt;b&gt;&amp;lt;entity match=&amp;quot;.*Addres.*|UnitMeasure|CreditCard&amp;quot;&amp;gt;&lt;/b&gt; is a selctor based on the entity name. The selector match attribute is a .NET Regex. All entities satisfied this Regex will be interested in the alter process. &lt;/p&gt;
&lt;p align="justify"&gt;The &lt;b&gt;&amp;lt;member-tag&amp;gt;&lt;/b&gt; is a selector, the match is done against the tag name in the generated hbm. The &lt;b&gt;exclude&lt;/b&gt; attribute means, in this case, to remove all the collections from the entity. If a name based selection is required, &lt;b&gt;&amp;lt;member-name&amp;gt;&lt;/b&gt; has to be used, with the same semantic.&lt;/p&gt;
&lt;p align="justify"&gt;The &lt;b&gt;alter tag&lt;/b&gt; drive the modification, and works both on a per-entity or a per-member base. In an alter tag the user can specify &lt;b&gt;&amp;lt;set name=&amp;rdquo;something&amp;rdquo; value=&amp;rdquo;val&amp;rdquo;/&amp;gt;&lt;/b&gt; to set ( or change ) an attribute on the generated hbm.&amp;nbsp; To remove an attribute the &lt;b&gt;&amp;lt;remove name=&amp;rdquo;toremove&amp;rdquo;/&amp;gt;&lt;/b&gt; is required, and for add an additional meta information on the hbm the &lt;b&gt;&amp;lt;meta-add attribute=&amp;rdquo;metaattribute&amp;rdquo;&amp;gt;the attribute content&amp;lt;/meta-attribute&amp;gt;&lt;/b&gt; has to be used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;entities-namespace&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Specify the namespace of the generated entities.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;entities-assembly&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Specify the assembly containing the generated entities. ( db2hbm does not compile any code, but this is a parameter in the hbm file).&lt;/p&gt;
&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Using the command line tool&lt;/h2&gt;
&lt;p&gt;Using the command line tool is very easy: just launch&lt;/p&gt;
&lt;p&gt;&lt;b&gt;db2hbm &amp;ndash;config:configfilename &amp;ndash;output:outputdir&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The output directory is optional. If not specified, hbm artifacts will be generated in the current folder.&lt;/p&gt;</description></item><item><title>How to use Db2hbm</title><link>http://nhforge.org/wikis/howtonh/how-to-use-db2hbm/revision/1.aspx</link><pubDate>Thu, 25 Feb 2010 13:05:08 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:318</guid><dc:creator>felicepollano</dc:creator><description>Revision 1 posted to How to by felicepollano on 25/02/2010 10:05:08 a.m.&lt;br /&gt;
&lt;p align="justify"&gt;Db2hbm is a reverse engineering tool able to generate hbm files from a database schema and some information contained in a configuration file. Unlike other commercial and open source tools serving the same purpose, db2hbm is not template based. The hbm output comes from a serialization based on the NHibernate mapping schema classes. Extendibility is provided through&amp;nbsp; strategies the user can implement or extend. db2hbm does not generate code. In order to generate the classes or other artifacts please consider the use of &lt;a href="/blogs/nhibernate/archive/2009/12/12/t4-hbm2net-alpha-2.aspx"&gt;hbm2net&lt;/a&gt;. &lt;/p&gt;
&lt;p align="justify"&gt;Db2hbm is a command line tool. &lt;a target="_blank" href="/media/p/615.aspx"&gt;You can download it here&lt;/a&gt;. Information on how to connect to the database and various generation options are contained in a separate configuration file you have to provide in order to make the tool work. A schema for the configuration file is provided in the distributed zip:&lt;b&gt;nhibernate-codegen.xsd.&lt;/b&gt; You will have intellisense editing the configuration file in Visual Studio by copying that file under the&lt;b&gt; %Program Files%\Microsoft Visual Studio 9.0\Xml\Schemas&lt;/b&gt; folder. Below there is a configuration file skeleton and a description for each section is provided.&lt;/p&gt;
&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;span style="font-family:consolas;background:yellow;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:yellow;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;xml&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;version&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;encoding&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:yellow;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:yellow;"&gt;?&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;db2hbm-conf&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;xmlns&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;urn:nhibernate-codegen-2.2&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;metadata-strategies&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.FirstPassEntityCollector, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.PrimaryKeyStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.ManyToOneStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.SetStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.ManyToManyStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.ApplyEntityExceptionsStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:green;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;!--strategy class=&amp;quot;NHibernate.Tool.Db2hbm.SetToMapStrategy, NHibernate.Tool.Db2hbm&amp;quot;/--&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/metadata-strategies&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;foreign-key-crawlers&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;factory&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;NHibernate.Tool.Db2hbm.ForeignKeyCrawlers.MSSQLForeignKeyCrawlerFactory, NHibernate.Tool.Db2hbm&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/factory&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/foreign-key-crawlers&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;type-mapping&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;xml&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:green;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;!--user type instead--&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;bit&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Boolean&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;varbinary&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Array&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;varchar&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;nvarchar&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;nchar&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;char&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;String&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;numeric&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;tinyint&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Byte&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;smallint&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Int16&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;int&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Int32&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;bigint&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Int64&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;datetime&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;DateTime&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;uniqueidentifier&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Guid&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;money&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;sql-type&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;dbtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;smallmoney&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;nhtype&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Decimal&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/type-mapping&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;naming-strategy&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;class&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;NHibernate.Tool.Db2hbm.DefaultNamingStrategy, NHibernate.Tool.Db2hbm&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;property&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;Language&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;English&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/naming-strategy&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;connection-info&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;dialect&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;NHibernate.Dialect.MsSql2005Dialect, NHibernate&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/dialect&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;connection-string&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;Server=localhost\SQLEXPRESS;initial catalog=AdventureWorks;Integrated Security=True&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/connection-string&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;connection-driver&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;NHibernate.Driver.SqlClientDriver, NHibernate&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/connection-driver&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/connection-info&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;table-filter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;include&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:green;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;!--Include all--&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;/table-filter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;tables&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;/tables&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&amp;lt;entity-exceptions&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:fr-be;" lang="FR-BE"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entity&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;.*Addres.*|UnitMeasure|CreditCard&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;member-tag&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;set|bag|idbag|list&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;exclude&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;set&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;lazy&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;value&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;lt;meta-add&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;attribute&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;quot;comment&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;this is a sample meta&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&amp;lt;/meta-add&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;mso-ansi-language:it;" lang="IT"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entity&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entity&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;.*&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;member-tag&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;set|bag|idbag|list&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;set&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;access&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;value&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;field.camelcase-underscore&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;meta-add&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;attribute&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;test&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;Value of test&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/meta-add&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/member-tag&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;member-tag&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;match&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;many-to-one&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;set&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;name&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;fetch&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:red;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;value&lt;/span&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;=&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:#8000ff;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;quot;join&amp;quot;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;/&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/alter&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/member-tag&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entity&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entity-exceptions&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entities-namespace&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;AdventureWorks.Entities&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entities-namespace&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p style="line-height:normal;margin-bottom:0pt;mso-layout-grid-align:none;" class="MsoNormal"&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;entities-assembly&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;AdventureWorks.Entities&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/entities-assembly&amp;gt;&lt;/span&gt;&lt;b&gt;&lt;span style="font-family:consolas;background:white;color:black;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height:115%;font-family:consolas;background:white;color:blue;font-size:8pt;mso-bidi-font-family:consolas;mso-highlight:white;"&gt;&amp;lt;/db2hbm-conf&amp;gt;&lt;/span&gt;&lt;span style="line-height:115%;font-size:8pt;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="line-height:115%;font-size:8pt;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;&amp;lt;metadata-strategies&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;This section configure the strategy used to infer the mapping from the schema or from what is inferred from the previous strategies. The execution order is preserved and reflect the order on the configuration file. The current version has the following strategy embedded:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;FirstPassEntityCollector&lt;/b&gt; &amp;ndash; Collect each table in the schema and provide an entity for each table with a property for each column. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;PrimaryKeyStrategy&lt;/b&gt; &amp;ndash; Find the primary keys on the tables and modify the previously collected entities to reflect the proper &amp;lt;id&amp;gt; or &amp;lt;composite-id&amp;gt; declared in the schema. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;ManyToOneStrategy&lt;/b&gt; &amp;ndash; Find the defined foreign keys, and if they point to mapped tables, creates the &amp;lt;many-to-one&amp;gt; associations &lt;/li&gt;
&lt;li&gt;&lt;b&gt;SetStrategy&lt;/b&gt; &amp;ndash; Make collections at the other end points of a &amp;lt;many-to-one&amp;gt; association. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;ManyToManyStrategy&lt;/b&gt; &amp;ndash; Try to infer if there is a link table between two entities. If so the associative entity is dropped and a pair of collection of type &amp;lt;bag&amp;gt; or &amp;lt;idbag&amp;gt; is created to represent the many-to-many association. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;ApplyEntityExceptionsStrategy&lt;/b&gt; &amp;ndash; Applies code generation exceptions to the entities according to the &lt;b&gt;&lt;i&gt;&amp;lt;entity-exceptions&amp;gt;&lt;/i&gt;&lt;/b&gt; configuration section( see below ) &lt;/li&gt;
&lt;/ul&gt;
&lt;p align="justify"&gt;Each strategy element, if required, can contain one of more element &amp;lt;property&amp;gt; in the followinf form:&lt;/p&gt;
&lt;p align="justify"&gt;&amp;lt;property name=&amp;quot;X&amp;quot;&amp;gt;yyyyyyy&amp;lt;/property&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;This means: set the value of the property named &amp;ldquo;X&amp;rdquo; to the value yyyyyy. Internally the value is converted to the target type of the property if it is not a string.&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;&amp;lt;foreign-key-crawlers&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;The core strategies used in reverse engineering requires to know how the foreign key are declared on the tables. Since the ADO.NET schema provider model does not fit this requirement, we have to provide a custom strategy for each DB dialect we are using. &lt;b&gt;&lt;span style="text-decoration:underline;"&gt;Currently only MSSQL server is supported&lt;/span&gt;&lt;/b&gt;, so if you want a proper code generation for another database you should provide your own foreign-key.-crawler, or alternatively configure each FK definition by hand, as shown in the &lt;b&gt;&amp;lt;tables&amp;gt;&lt;/b&gt; configuration section ( you would probably prefer not to ).&lt;/p&gt;
&lt;p align="justify"&gt;To provide your own key crawler, you have to create a factory for it, implementing the interface &lt;b&gt;IForeignKeyCrawlerFactory&lt;/b&gt;, declared as shown below:&lt;/p&gt;
&lt;p&gt;public interface IForeignKeyCrawlerFactory    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IForeignKeyCrawler Create();     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void Register();     &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The register method is called by the infrastructure, and allow you to bind your factory with one or more NHIbernate Dialect. Let&amp;rsquo;s have an example on how the MSSQLForeignKeyCrawlerFactory implement this function:&lt;/p&gt;
&lt;p&gt;public void Register()    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var instance = new MSSQLForeignKeyCrawlerFactory();     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForeignKeyCrawlersRegistar.Register(instance, typeof(MsSql2000Dialect));     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForeignKeyCrawlersRegistar.Register(instance, typeof(MsSql2005Dialect));     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForeignKeyCrawlersRegistar.Register(instance, typeof(MsSql2008Dialect));     &lt;br /&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;The&amp;nbsp; code above register an instance of MSSQLForeignKeyCrawlerFactory bound to the MsSql200x dialects.&lt;/p&gt;
&lt;p align="justify"&gt;The other method, Create, are supposed to return the IForeignCrawler instance able to deal with the specific database foreign key schema info. This object must implement the following interface:&lt;/p&gt;
&lt;p&gt;public interface IForeignKeyCrawler {    &lt;br /&gt;IForeignKeyColumnInfo[] GetForeignKeyColumns(DbConnection dbConnection     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , string constraintName     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , string catalog     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , string schema     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; );     &lt;br /&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;To proper implement this you should be able to return an array of &lt;b&gt;IForeignKeyColumnInfo&lt;/b&gt; from the constraint name, the catalog and the schema containing it.&lt;/p&gt;
&lt;p align="justify"&gt;The I&lt;b&gt;ForeignKeyColumnInfo&lt;/b&gt; interface is defined as follow:&lt;/p&gt;
&lt;p&gt;public interface IForeignKeyColumnInfo    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyColumnName { get; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyTableName { get; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyTableSchema { get;&amp;nbsp; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string PrimaryKeyTableCatalog { get;&amp;nbsp; } &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyColumnName { get; }    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyTableName { get; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyTableSchema { get;&amp;nbsp; }     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string ForeignKeyTableCatalog { get;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;&lt;b&gt;PrimaryKey&lt;/b&gt;/(Table/Catalog/Column)&lt;b&gt;Name&lt;/b&gt; must return the key column information as defined in the table containing the primary key referenced by the constraint.&lt;/p&gt;
&lt;p align="justify"&gt;&lt;b&gt;ForeignKey&lt;/b&gt;/(Table/Catalog/Column)&lt;b&gt;Name&lt;/b&gt; must return the key column information as defined in the table containing the foreign key referenced by the constraint.&lt;/p&gt;
&lt;h2&gt;&amp;nbsp;&lt;/h2&gt;
&lt;h2&gt;&amp;lt;type-mapping&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Each element in this section maps a database type to an NHIbernate type. You can force a type with respect to column length/precision too. So you can have:&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&amp;lt;sql-type dbtype=&amp;quot;varchar&amp;quot; length=&amp;quot;1&amp;quot; nhtype=&amp;quot;Char&amp;quot;/&amp;gt;      &lt;br /&gt;&amp;lt;sql-type dbtype=&amp;quot;varchar&amp;quot; length=&amp;quot;2-*&amp;quot; nhtype=&amp;quot;String&amp;quot;/&amp;gt;&lt;/b&gt;&lt;/p&gt;
&lt;p align="justify"&gt;in order to distinguish between char and strings. The most restrictive satisfied condition is used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;naming-strategy&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;The naming strategy is a component driving the name generation for the entities, properties, collections and component key class generated by the tool. A default naming strategy is provided: &lt;b&gt;NHibernate.Tool.Db2hbm.DefaultNamingStrategy&lt;/b&gt;. This strategy leverages the unNHAddins inflector to singularize the name of the tables, or pluralize the collections. The unNHaddins inflector is provided for many languages, the DefaultNamingStrategy infer the concrete class to use from the property language:&lt;/p&gt;
&lt;p&gt;&amp;lt;property name=&amp;quot;Language&amp;quot;&amp;gt;English&amp;lt;/property&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;If an unknown language is specified, DefaultNamingStrategy uses &amp;ldquo;English &amp;ldquo; by default. In order to implement your own naming strategy, you have to derive from the default naming strategy, or implement your own from scratch by implementing the &lt;b&gt;INamingStrategy interface&lt;/b&gt;. Please note that despites the name, this interface &lt;span style="text-decoration:underline;"&gt;is not the one contained in NHibernate&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The interface is defined as follow:&lt;/p&gt;
&lt;p&gt;public interface INamingStrategy    &lt;br /&gt;{     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetEntityNameFromTableName(string tableName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetPropertyNameFromColumnName(string columnName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetIdPropertyNameFromColumnName(string columnName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetClassNameForComponentKey(string entityName);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetNameForComponentKey(string entityName,string componentClass);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetNameForManyToOne(string referredEntity, string[] columnNames);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetNameForCollection(string collectingClass, int progressive);     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string GetClassNameForCollectionComponent(string collectionTableName);     &lt;br /&gt;}&lt;/p&gt;
&lt;p align="justify"&gt;The meaning of the first three functions should be clear by the name. For the other ones:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetClassNameForComponentKey&lt;/b&gt; &amp;ndash; The entity has a composite key, and it is reverse engineering with a component, implementor has to return a name for the class representing that component. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetNameForManyToOne&lt;/b&gt; &amp;ndash; The entity has a reference to another entity, you have to return the name you want to assign to the corresponding property. Implementor will receive the referred entity name and the column(s) names defining the reference. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetNameForCollection&lt;/b&gt; &amp;ndash; the entity has a collection of other entity instances. The implementor will receive the name of the collected entity, plus a progressive ( you may have more collection of the same type of entity in a class ). implementor has to return the name to assign to the property representhing the collection. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;GetClassNameForCollectionComponent&lt;/b&gt; &amp;ndash; There is a map of component. implementor has to return the name of the class representing the component. &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;connection-info&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Db2hbm needs to connect to the database to inquire into schema information. In this section user will provide the essential parameters to achieve the connection.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;&amp;lt;dialect&amp;gt;&lt;/b&gt; &amp;ndash; the NHibernate dialect class. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;&amp;lt;connection-string&amp;gt;&lt;/b&gt; &amp;ndash; the DB connection string &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div align="justify"&gt;&lt;b&gt;&amp;lt;connection-driver&amp;gt;&lt;/b&gt; &amp;ndash; the NHibernate connection driver &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;table-filter&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;User can specify which table to include/exclude from the reverse engineering properties by adding keys to this section.&lt;/p&gt;
&lt;p align="justify"&gt;More than one include/exclude element can be specified. Each element has three attributes: &lt;/p&gt;
&lt;p align="justify"&gt;&amp;lt;include catalog=&amp;quot;.*&amp;quot; schema=&amp;quot;.*&amp;quot; table=&amp;quot;.*&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;&amp;lt;exclude table=&amp;quot;UselessTable&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p align="justify"&gt;you can specify a regular expression to specify the pattern for the catalog, the schema and the table. Tables matching this pattern will be excluded/included.&lt;/p&gt;
&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;tables&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;If there is not enougth information in the schema, or if there is not a proper foreign key crawler available for the database in use, it is possible to specify manually the foreign key/primary key layout. These information will merge with the ones already present in the schema, if any.&lt;/p&gt;
&lt;p&gt;Here below an example:&lt;/p&gt;
&lt;p&gt;&amp;lt;tables&amp;gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;table schema=&amp;quot;dbo&amp;quot; catalog=&amp;quot;db2hbm&amp;quot; name=&amp;quot;Simple&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;primary-key &amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;generator class=&amp;quot;sequence&amp;quot; &amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name=&amp;quot;sequence&amp;quot;&amp;gt;id_seq&amp;lt;/param&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/generator&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/primary-key&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;foreign-key constraint-name=&amp;quot;myconstraint&amp;quot; foreign-catalog=&amp;quot;adatabase&amp;quot; foreign-schema=&amp;quot;dbo&amp;quot; foreign-table=&amp;quot;ReferredTable&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;column-ref foreign-column=&amp;quot;id&amp;quot; local-column=&amp;quot;referred-id&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/foreign-key&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/table&amp;gt;     &lt;br /&gt;&amp;nbsp; &amp;lt;/tables&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;entity-exceptions&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;In this section the user can finely drive the produced hbm artifacts by altering the default tool behavior on a per-name o per-tag based strategy. Let&amp;rsquo;s have an example:&lt;/p&gt;
&lt;p&gt;&amp;lt;entity-exceptions&amp;gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;entity match=&amp;quot;.*Addres.*|UnitMeasure|CreditCard&amp;quot;&amp;nbsp; &amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;member-tag match=&amp;quot;set|bag|idbag|list&amp;quot; exclude=&amp;quot;true&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;set name=&amp;quot;lazy&amp;quot; value=&amp;quot;false&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;meta-add attribute=&amp;quot;comment&amp;quot;&amp;gt;a comment&amp;hellip;&amp;lt;/meta-add&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/entity&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;entity match=&amp;quot;.*&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;member-tag match=&amp;quot;set|bag|idbag|list&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;set name=&amp;quot;access&amp;quot; value=&amp;quot;field.camelcase-underscore&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;meta-add attribute=&amp;quot;test&amp;quot;&amp;gt;Value of test&amp;lt;/meta-add&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/member-tag&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;member-tag match=&amp;quot;many-to-one&amp;quot;&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;set name=&amp;quot;fetch&amp;quot; value=&amp;quot;join&amp;quot;/&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/alter&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/member-tag&amp;gt;     &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/entity&amp;gt;     &lt;br /&gt;&amp;lt;/entity-exceptions&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p align="justify"&gt;The tag &lt;b&gt;&amp;lt;entity match=&amp;quot;.*Addres.*|UnitMeasure|CreditCard&amp;quot;&amp;gt;&lt;/b&gt; is a selctor based on the entity name. The selector match attribute is a .NET Regex. All entities satisfied this Regex will be interested in the alter process. &lt;/p&gt;
&lt;p align="justify"&gt;The &lt;b&gt;&amp;lt;member-tag&amp;gt;&lt;/b&gt; is a selector, the match is done against the tag name in the generated hbm. The &lt;b&gt;exclude&lt;/b&gt; attribute means, in this case, to remove all the collections from the entity. If a name based selection is required, &lt;b&gt;&amp;lt;member-name&amp;gt;&lt;/b&gt; has to be used, with the same semantic.&lt;/p&gt;
&lt;p align="justify"&gt;The &lt;b&gt;alter tag&lt;/b&gt; drive the modification, and works both on a per-entity or a per-member base. In an alter tag the user can specify &lt;b&gt;&amp;lt;set name=&amp;rdquo;something&amp;rdquo; value=&amp;rdquo;val&amp;rdquo;/&amp;gt;&lt;/b&gt; to set ( or change ) an attribute on the generated hbm.&amp;nbsp; To remove an attribute the &lt;b&gt;&amp;lt;remove name=&amp;rdquo;toremove&amp;rdquo;/&amp;gt;&lt;/b&gt; is required, and for add an additional meta information on the hbm the &lt;b&gt;&amp;lt;meta-add attribute=&amp;rdquo;metaattribute&amp;rdquo;&amp;gt;the attribute content&amp;lt;/meta-attribute&amp;gt;&lt;/b&gt; has to be used.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;entities-namespace&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Specify the namespace of the generated entities.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&amp;lt;entities-assembly&amp;gt;&lt;/h2&gt;
&lt;p align="justify"&gt;Specify the assembly containing the generated entities. ( db2hbm does not compile any code, but this is a parameter in the hbm file).&lt;/p&gt;
&lt;p align="justify"&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Using the command line tool&lt;/h2&gt;
&lt;p&gt;Using the command line tool is very easy: just launch&lt;/p&gt;
&lt;p&gt;&lt;b&gt;db2hbm &amp;ndash;config:configfilename &amp;ndash;output:outputdir&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The output directory is optional. If not specified, hbm artifacts will be generated in the current folder.&lt;/p&gt;</description></item><item><title>Customizing Fluent Nhibernate's AutoPersistenceModel Conventions</title><link>http://nhforge.org/wikis/howtonh/customizing-fluent-nhibernate-s-autopersistencemodel-conventions/revision/0.aspx</link><pubDate>Fri, 25 Dec 2009 17:04:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:69</guid><dc:creator>Chris Nicola</dc:creator><description>Current revision posted to How to by Chris Nicola on 25/12/2009 02:04:15 p.m.&lt;br /&gt;
&lt;h2&gt;Customizing Fluent Nhibernate&amp;#39;s AutoPersistenceModel Conventions&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FluentNH&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AutoPersistenceModel&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;convention&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;over&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;configuration&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="http://www.lucisferre.net/image.axd?picture=xmlschool_1.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;margin:0px 6px 0px 0px;display:inline;border-top:0px;border-right:0px;" title="xmlschool" src="http://www.lucisferre.net/image.axd?picture=xmlschool_thumb.jpg" alt="xmlschool" align="left" border="0" width="184" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(this was originally blogged about &lt;a href="http://www.lucisferre.net/post.aspx?id=0c0eb379-f939-406b-bfb9-a1130a5be664"&gt;here&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;One of the things I get a fair bit of use out of is the fluent nHibernate project and the AutoPersistenceModel.&amp;nbsp; If you are not very familiar with fluentNH or AutoPersistenceModel then I would suggest checking out their &lt;a href="http://wiki.fluentnhibernate.org/Main_Page"&gt;wiki&lt;/a&gt;, as what I am about to discuss, while not difficult, is some relatively advanced usage.&lt;/p&gt;
&lt;p&gt;The purpose of AutoPersistenceModel is to automatically generate the nHibernate configuration (the HBM files if you are currently used to XML configuration) directly from your model based on Convention over Configuration, &lt;a href="http://www.jameskovacs.com/blog/NETRocks475JamesKovacsOnConventionoverConfiguration.aspx"&gt;you can hear James Kovacs discuss this concept on .NET Rocks&lt;/a&gt;.&amp;nbsp; With tools like SchemaExport and SchemaUpdate it is even possible to automatically generate and execute DDL scripts against your database to keep the schema in sync with your model.&amp;nbsp; This can be a bit rails like in it&amp;#39;s use, in fact &lt;a href="http://adventuresinagile.blogspot.com/"&gt;Adam Dymitruk&lt;/a&gt; is currently working on a utility to extend the SchemaUpdate to allow the creation of versioned database scripts like rails has (ok Adam it&amp;#39;s official, now you actually &lt;i&gt;have&lt;/i&gt; &lt;i&gt;to &lt;/i&gt;finish it!).&lt;/p&gt;
&lt;p&gt;The default AutoPersistenceModel conventions are kept quite simple, so often you will need to do a bit of customization.&amp;nbsp; One way to do this is through overrides which I showed how to use in &lt;a href="http://www.lucisferre.net/post.aspx?id=1734653d-6df8-4aa8-8aef-c264406ced37"&gt;this post&lt;/a&gt;, were I implemented an override for a self-referencing tree relationship.&amp;nbsp; However, whenever possible, it is preferable to use fluentNH&amp;#39;s conventions to do this.&amp;nbsp; Conventions can be used define custom behavior in very flexible ways.&amp;nbsp; Below I am going to show how this can be done to customize database constraints like &lt;i&gt;unique &lt;/i&gt;and &lt;i&gt;index&lt;/i&gt;. [more]&lt;/p&gt;
&lt;p&gt;S#arp Architecture provides a &lt;a href="http://wiki.sharparchitecture.net/Tutorial2DevDomainModel.ashx"&gt;[DomainSignature] attribute&lt;/a&gt; which you can apply to your entity&amp;#39;s properties.&amp;nbsp;&amp;nbsp; The attribute is used to denote the set of properties that uniquely define the entity and is similar to the concept of a &lt;i&gt;business key&lt;/i&gt; often used in SQL database design.&amp;nbsp; It is important to point out that the &lt;i&gt;DomainSignature should&lt;/i&gt;&amp;nbsp;&lt;i&gt;not be considered a primary key &lt;/i&gt;and that you should &lt;i&gt;always use a surrogate primary key&lt;/i&gt; generated either by the database or nHibernate (hilo and guid.comb are the two I prefer).&lt;/p&gt;
&lt;p&gt;The domain signature can ensures that entities can be compared using the properties decorated with [DomainSignature].&amp;nbsp; This is useful if say one object was loaded from the repository using nHibernate but another was constructed and I want to determine if I should treat them as the same entity.&amp;nbsp; It is also useful to determine if a new entity will violate a uniqueness constraint you want to enforce.&lt;/p&gt;
&lt;p&gt;A good example of a useful DomainSignature would be the slug of a blog post.&amp;nbsp; A post also has an &lt;i&gt;id &lt;/i&gt;for it&amp;#39;s primary key&lt;i&gt;,&lt;/i&gt; in most cases a guid, but the slug also uniquely identifies a post as well and no two posts can have the same slug.&amp;nbsp; The only problem now is that when I generate my DDL I can see that nHibernate has no notion that it should enforce my uniqueness constraint.&lt;/p&gt;
&lt;p&gt;What I want is for nHibernate and hence the database schema should be aware that DomainSignature implies a uniqueness constraint.&amp;nbsp; Fortunately, fluent nHibernate conventions make this is quite easy.&amp;nbsp; Fluent nHibernate defines an &lt;i&gt;AttributePropertyConvention&amp;lt;T&amp;gt;&lt;/i&gt; base class for exactly this purpose and we can extend it like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5cfc2d16-0a65-4872-b026-c82df251e9a3" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DomainSignatureConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;AttributePropertyConvention&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;&amp;gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt; attribute, &lt;span style="color:#2b91af;"&gt;IPropertyInstance&lt;/span&gt; instance) { &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(instance.EntityType.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The DomainSignatureConvention tells fluent that all properties that are decorated with [DomainSignature] should form a unique key.&amp;nbsp; Now if I then define my entity like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d54e305c-c2cc-4f98-a45c-8a3cb5f7b64a" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Price&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Entity&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Security&lt;/span&gt; Security { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt; PriceDate { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; CanadianPrice { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Bid { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Ask { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Close { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;then SchemaExport will generate DDL like this:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style:none;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; Prices (&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;    Id &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;not&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;,&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   PriceDate DATETIME,&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   CanadianPrice &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt;,&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   Bid &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   Ask &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   &lt;span style="color:#0000ff;"&gt;Close&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   SecurityFk &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt;,&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   &lt;span style="color:#0000ff;"&gt;primary&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;key&lt;/span&gt; (Id),&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;  &lt;span style="color:#0000ff;"&gt;unique&lt;/span&gt; (PriceDate, CanadianPrice)&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;)&lt;/pre&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--CRLF--&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Except we have a small problem here.&amp;nbsp; The foreign key for the many-to-one relationship on &lt;i&gt;Security&lt;/i&gt; was not included in the &lt;i&gt;unique&lt;/i&gt; constraint.&amp;nbsp; To be quite honest I am not exactly sure why but I am guessing that the &lt;i&gt;AttributePropertyConvention &lt;/i&gt;does not work with a reference.&amp;nbsp; Instead I will need to add something to my &lt;i&gt;ReferenceConvention&lt;/i&gt; which is provided by default with s#arp architecture.&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2787227c-6879-4cc5-8c9a-f06d171b4095" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReferenceConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IReferenceConvention&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(FluentNHibernate.Conventions.Instances.&lt;span style="color:#2b91af;"&gt;IManyToOneInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Column(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Fk&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;.IsDefined(instance.Property, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;)))&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(&lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I don&amp;#39;t really like this as I don&amp;#39;t think the attribute should be ignored, but it does at least change the above to &lt;i&gt;unique(PriceDate, CanadianPrice, SecurityFk).&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;There are obviously many other types of constraints we could use, you could create an attribute for defining indexing on certain properties like [Indexable(&amp;quot;IndexName&amp;quot;)] and create a convention like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dc415913-baed-4310-ae51-951f5c4fcec1" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; _name;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; IndexableAttribute(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name) { _name = name; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; GetName() { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _name; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IndexableConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;AttributePropertyConvention&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt;&amp;gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(&lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt; attribute, &lt;span style="color:#2b91af;"&gt;IPropertyInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Index(attribute.GetName());&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now I can create indicies by using the [Indexable(&amp;quot;Name&amp;quot;)] attribute.&amp;nbsp; Properties with the same &amp;quot;Name&amp;quot; will be part of the same index constraint and indexed together.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I found it is actually a good idea to always index many-to-one relationships, something I briefly mentioned in a &lt;a href="http://www.lucisferre.net/post.aspx?id=ef779300-206b-4582-a9d0-4275b328c8c9"&gt;previous post&lt;/a&gt;.&amp;nbsp; Now that I am using fluent nhibernate however I can&amp;#39;t set the index property in the HBM files so I will need a convention for that.&amp;nbsp; It also makes sense to set this indexing on all many-to-one relationships so there is a better way to do this that does not involve the use of an attribute, instead we implement IRefrenceConvention.&amp;nbsp; S#arp architecture already includes this convention by default so we can just edit it:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0bc6d7e9-4abf-4c45-b26d-85fa8e16fca0" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReferenceConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IReferenceConvention&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(FluentNHibernate.Conventions.Instances.&lt;span style="color:#2b91af;"&gt;IManyToOneInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Column(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Fk&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;.IsDefined(instance.Property, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;)))&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(&lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt; &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Index(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Notice the if.else, if we have already defined &lt;i&gt;UniqueKey&lt;/i&gt; defining Index would be redundant as unique key&amp;#39;s are already indexed.&amp;nbsp; When defining indexes you will typically see something like like the following DDL output from SchemaExport:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre id="codeSnippet" style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;index&lt;/span&gt; SecurityIndex &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; Prices (SecurityFk)&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;p&gt;It is pretty easy to customize the behavior of the fluent nhibernate AutoPersistenceModel using conventions and I find it can be very useful to have this type of fine grained control over your database schema generation.&lt;/p&gt;</description></item><item><title>Customizing Fluent Nhibernate's AutoPersistenceModel Conventions</title><link>http://nhforge.org/wikis/howtonh/customizing-fluent-nhibernate-s-autopersistencemodel-conventions/revision/2.aspx</link><pubDate>Fri, 25 Dec 2009 00:38:05 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:306</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 2 posted to How to by Chris Nicola on 24/12/2009 09:38:05 p.m.&lt;br /&gt;
&lt;h2&gt;Customizing Fluent Nhibernate&amp;#39;s AutoPersistenceModel Conventions&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;Mapping&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; FluentNH, AutoPersistenceModel, convention over configuration&lt;/div&gt;

&lt;p&gt;&lt;a href="http://www.lucisferre.net/image.axd?picture=xmlschool_1.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;margin:0px 6px 0px 0px;display:inline;border-top:0px;border-right:0px;" title="xmlschool" src="http://www.lucisferre.net/image.axd?picture=xmlschool_thumb.jpg" alt="xmlschool" align="left" border="0" width="184" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(this was originally blogged about &lt;a href="http://www.lucisferre.net/post.aspx?id=0c0eb379-f939-406b-bfb9-a1130a5be664"&gt;here&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;One of the things I get a fair bit of use out of is the fluent nHibernate project and the AutoPersistenceModel.&amp;nbsp; If you are not very familiar with fluentNH or AutoPersistenceModel then I would suggest checking out their &lt;a href="http://wiki.fluentnhibernate.org/Main_Page"&gt;wiki&lt;/a&gt;, as what I am about to discuss, while not difficult, is some relatively advanced usage.&lt;/p&gt;
&lt;p&gt;The purpose of AutoPersistenceModel is to automatically generate the nHibernate configuration (the HBM files if you are currently used to XML configuration) directly from your model based on Convention over Configuration, &lt;a href="http://www.jameskovacs.com/blog/NETRocks475JamesKovacsOnConventionoverConfiguration.aspx"&gt;you can hear James Kovacs discuss this concept on .NET Rocks&lt;/a&gt;.&amp;nbsp; With tools like SchemaExport and SchemaUpdate it is even possible to automatically generate and execute DDL scripts against your database to keep the schema in sync with your model.&amp;nbsp; This can be a bit rails like in it&amp;#39;s use, in fact &lt;a href="http://adventuresinagile.blogspot.com/"&gt;Adam Dymitruk&lt;/a&gt; is currently working on a utility to extend the SchemaUpdate to allow the creation of versioned database scripts like rails has (ok Adam it&amp;#39;s official, now you actually &lt;i&gt;have&lt;/i&gt; &lt;i&gt;to &lt;/i&gt;finish it!).&lt;/p&gt;
&lt;p&gt;The default AutoPersistenceModel conventions are kept quite simple, so often you will need to do a bit of customization.&amp;nbsp; One way to do this is through overrides which I showed how to use in &lt;a href="http://www.lucisferre.net/post.aspx?id=1734653d-6df8-4aa8-8aef-c264406ced37"&gt;this post&lt;/a&gt;, were I implemented an override for a self-referencing tree relationship.&amp;nbsp; However, whenever possible, it is preferable to use fluentNH&amp;#39;s conventions to do this.&amp;nbsp; Conventions can be used define custom behavior in very flexible ways.&amp;nbsp; Below I am going to show how this can be done to customize database constraints like &lt;i&gt;unique &lt;/i&gt;and &lt;i&gt;index&lt;/i&gt;. [more]&lt;/p&gt;
&lt;p&gt;S#arp Architecture provides a &lt;a href="http://wiki.sharparchitecture.net/Tutorial2DevDomainModel.ashx"&gt;[DomainSignature] attribute&lt;/a&gt; which you can apply to your entity&amp;#39;s properties.&amp;nbsp;&amp;nbsp; The attribute is used to denote the set of properties that uniquely define the entity and is similar to the concept of a &lt;i&gt;business key&lt;/i&gt; often used in SQL database design.&amp;nbsp; It is important to point out that the &lt;i&gt;DomainSignature should&lt;/i&gt;&amp;nbsp;&lt;i&gt;not be considered a primary key &lt;/i&gt;and that you should &lt;i&gt;always use a surrogate primary key&lt;/i&gt; generated either by the database or nHibernate (hilo and guid.comb are the two I prefer).&lt;/p&gt;
&lt;p&gt;The domain signature can ensures that entities can be compared using the properties decorated with [DomainSignature].&amp;nbsp; This is useful if say one object was loaded from the repository using nHibernate but another was constructed and I want to determine if I should treat them as the same entity.&amp;nbsp; It is also useful to determine if a new entity will violate a uniqueness constraint you want to enforce.&lt;/p&gt;
&lt;p&gt;A good example of a useful DomainSignature would be the slug of a blog post.&amp;nbsp; A post also has an &lt;i&gt;id &lt;/i&gt;for it&amp;#39;s primary key&lt;i&gt;,&lt;/i&gt; in most cases a guid, but the slug also uniquely identifies a post as well and no two posts can have the same slug.&amp;nbsp; The only problem now is that when I generate my DDL I can see that nHibernate has no notion that it should enforce my uniqueness constraint.&lt;/p&gt;
&lt;p&gt;What I want is for nHibernate and hence the database schema should be aware that DomainSignature implies a uniqueness constraint.&amp;nbsp; Fortunately, fluent nHibernate conventions make this is quite easy.&amp;nbsp; Fluent nHibernate defines an &lt;i&gt;AttributePropertyConvention&amp;lt;T&amp;gt;&lt;/i&gt; base class for exactly this purpose and we can extend it like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5cfc2d16-0a65-4872-b026-c82df251e9a3" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DomainSignatureConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;AttributePropertyConvention&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;&amp;gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt; attribute, &lt;span style="color:#2b91af;"&gt;IPropertyInstance&lt;/span&gt; instance) { &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(instance.EntityType.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The DomainSignatureConvention tells fluent that all properties that are decorated with [DomainSignature] should form a unique key.&amp;nbsp; Now if I then define my entity like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d54e305c-c2cc-4f98-a45c-8a3cb5f7b64a" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Price&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Entity&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Security&lt;/span&gt; Security { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt; PriceDate { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; CanadianPrice { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Bid { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Ask { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Close { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;then SchemaExport will generate DDL like this:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style:none;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; Prices (&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;    Id &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;not&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   PriceDate DATETIME,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   CanadianPrice &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   Bid &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   Ask &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   &lt;span style="color:#0000ff;"&gt;Close&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   SecurityFk &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   &lt;span style="color:#0000ff;"&gt;primary&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;key&lt;/span&gt; (Id),&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;  &lt;span style="color:#0000ff;"&gt;unique&lt;/span&gt; (PriceDate, CanadianPrice)&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;)&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Except we have a small problem here.&amp;nbsp; The foreign key for the many-to-one relationship on &lt;i&gt;Security&lt;/i&gt; was not included in the &lt;i&gt;unique&lt;/i&gt; constraint.&amp;nbsp; To be quite honest I am not exactly sure why but I am guessing that the &lt;i&gt;AttributePropertyConvention &lt;/i&gt;does not work with a reference.&amp;nbsp; Instead I will need to add something to my &lt;i&gt;ReferenceConvention&lt;/i&gt; which is provided by default with s#arp architecture.&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2787227c-6879-4cc5-8c9a-f06d171b4095" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReferenceConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IReferenceConvention&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(FluentNHibernate.Conventions.Instances.&lt;span style="color:#2b91af;"&gt;IManyToOneInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Column(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Fk&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;.IsDefined(instance.Property, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;)))&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(&lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I don&amp;#39;t really like this as I don&amp;#39;t think the attribute should be ignored, but it does at least change the above to &lt;i&gt;unique(PriceDate, CanadianPrice, SecurityFk).&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;There are obviously many other types of constraints we could use, you could create an attribute for defining indexing on certain properties like [Indexable(&amp;quot;IndexName&amp;quot;)] and create a convention like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dc415913-baed-4310-ae51-951f5c4fcec1" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; _name;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; IndexableAttribute(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name) { _name = name; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; GetName() { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _name; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IndexableConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;AttributePropertyConvention&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt;&amp;gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(&lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt; attribute, &lt;span style="color:#2b91af;"&gt;IPropertyInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Index(attribute.GetName());&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now I can create indicies by using the [Indexable(&amp;quot;Name&amp;quot;)] attribute.&amp;nbsp; Properties with the same &amp;quot;Name&amp;quot; will be part of the same index constraint and indexed together.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I found it is actually a good idea to always index many-to-one relationships, something I briefly mentioned in a &lt;a href="http://www.lucisferre.net/post.aspx?id=ef779300-206b-4582-a9d0-4275b328c8c9"&gt;previous post&lt;/a&gt;.&amp;nbsp; Now that I am using fluent nhibernate however I can&amp;#39;t set the index property in the HBM files so I will need a convention for that.&amp;nbsp; It also makes sense to set this indexing on all many-to-one relationships so there is a better way to do this that does not involve the use of an attribute, instead we implement IRefrenceConvention.&amp;nbsp; S#arp architecture already includes this convention by default so we can just edit it:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0bc6d7e9-4abf-4c45-b26d-85fa8e16fca0" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReferenceConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IReferenceConvention&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(FluentNHibernate.Conventions.Instances.&lt;span style="color:#2b91af;"&gt;IManyToOneInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Column(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Fk&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;.IsDefined(instance.Property, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;)))&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(&lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt; &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Index(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Notice the if.else, if we have already defined &lt;i&gt;UniqueKey&lt;/i&gt; defining Index would be redundant as unique key&amp;#39;s are already indexed.&amp;nbsp; When defining indexes you will typically see something like like the following DDL output from SchemaExport:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre id="codeSnippet" style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;index&lt;/span&gt; SecurityIndex &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; Prices (SecurityFk)&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;p&gt;It is pretty easy to customize the behavior of the fluent nhibernate AutoPersistenceModel using conventions and I find it can be very useful to have this type of fine grained control over your database schema generation.&lt;/p&gt;</description></item><item><title>Customizing Fluent Nhibernate's AutoPersistenceModel Conventions</title><link>http://nhforge.org/wikis/howtonh/customizing-fluent-nhibernate-s-autopersistencemodel-conventions/revision/1.aspx</link><pubDate>Fri, 25 Dec 2009 00:35:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:305</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 1 posted to How to by Chris Nicola on 24/12/2009 09:35:49 p.m.&lt;br /&gt;
&lt;p&gt;&lt;a href="http://www.lucisferre.net/image.axd?picture=xmlschool_1.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;margin:0px 6px 0px 0px;display:inline;border-top:0px;border-right:0px;" title="xmlschool" src="http://www.lucisferre.net/image.axd?picture=xmlschool_thumb.jpg" alt="xmlschool" align="left" border="0" width="184" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(this was originally blogged about &lt;a href="http://www.lucisferre.net/post.aspx?id=0c0eb379-f939-406b-bfb9-a1130a5be664"&gt;here&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;One of the things I get a fair bit of use out of is the fluent nHibernate project and the AutoPersistenceModel.&amp;nbsp; If you are not very familiar with fluentNH or AutoPersistenceModel then I would suggest checking out their &lt;a href="http://wiki.fluentnhibernate.org/Main_Page"&gt;wiki&lt;/a&gt;, as what I am about to discuss, while not difficult, is some relatively advanced usage.&lt;/p&gt;
&lt;p&gt;The purpose of AutoPersistenceModel is to automatically generate the nHibernate configuration (the HBM files if you are currently used to XML configuration) directly from your model based on Convention over Configuration, &lt;a href="http://www.jameskovacs.com/blog/NETRocks475JamesKovacsOnConventionoverConfiguration.aspx"&gt;you can hear James Kovacs discuss this concept on .NET Rocks&lt;/a&gt;.&amp;nbsp; With tools like SchemaExport and SchemaUpdate it is even possible to automatically generate and execute DDL scripts against your database to keep the schema in sync with your model.&amp;nbsp; This can be a bit rails like in it&amp;#39;s use, in fact &lt;a href="http://adventuresinagile.blogspot.com/"&gt;Adam Dymitruk&lt;/a&gt; is currently working on a utility to extend the SchemaUpdate to allow the creation of versioned database scripts like rails has (ok Adam it&amp;#39;s official, now you actually &lt;i&gt;have&lt;/i&gt; &lt;i&gt;to &lt;/i&gt;finish it!).&lt;/p&gt;
&lt;p&gt;The default AutoPersistenceModel conventions are kept quite simple, so often you will need to do a bit of customization.&amp;nbsp; One way to do this is through overrides which I showed how to use in &lt;a href="http://www.lucisferre.net/post.aspx?id=1734653d-6df8-4aa8-8aef-c264406ced37"&gt;this post&lt;/a&gt;, were I implemented an override for a self-referencing tree relationship.&amp;nbsp; However, whenever possible, it is preferable to use fluentNH&amp;#39;s conventions to do this.&amp;nbsp; Conventions can be used define custom behavior in very flexible ways.&amp;nbsp; Below I am going to show how this can be done to customize database constraints like &lt;i&gt;unique &lt;/i&gt;and &lt;i&gt;index&lt;/i&gt;. [more]&lt;/p&gt;
&lt;p&gt;S#arp Architecture provides a &lt;a href="http://wiki.sharparchitecture.net/Tutorial2DevDomainModel.ashx"&gt;[DomainSignature] attribute&lt;/a&gt; which you can apply to your entity&amp;#39;s properties.&amp;nbsp;&amp;nbsp; The attribute is used to denote the set of properties that uniquely define the entity and is similar to the concept of a &lt;i&gt;business key&lt;/i&gt; often used in SQL database design.&amp;nbsp; It is important to point out that the &lt;i&gt;DomainSignature should&lt;/i&gt;&amp;nbsp;&lt;i&gt;not be considered a primary key &lt;/i&gt;and that you should &lt;i&gt;always use a surrogate primary key&lt;/i&gt; generated either by the database or nHibernate (hilo and guid.comb are the two I prefer).&lt;/p&gt;
&lt;p&gt;The domain signature can ensures that entities can be compared using the properties decorated with [DomainSignature].&amp;nbsp; This is useful if say one object was loaded from the repository using nHibernate but another was constructed and I want to determine if I should treat them as the same entity.&amp;nbsp; It is also useful to determine if a new entity will violate a uniqueness constraint you want to enforce.&lt;/p&gt;
&lt;p&gt;A good example of a useful DomainSignature would be the slug of a blog post.&amp;nbsp; A post also has an &lt;i&gt;id &lt;/i&gt;for it&amp;#39;s primary key&lt;i&gt;,&lt;/i&gt; in most cases a guid, but the slug also uniquely identifies a post as well and no two posts can have the same slug.&amp;nbsp; The only problem now is that when I generate my DDL I can see that nHibernate has no notion that it should enforce my uniqueness constraint.&lt;/p&gt;
&lt;p&gt;What I want is for nHibernate and hence the database schema should be aware that DomainSignature implies a uniqueness constraint.&amp;nbsp; Fortunately, fluent nHibernate conventions make this is quite easy.&amp;nbsp; Fluent nHibernate defines an &lt;i&gt;AttributePropertyConvention&amp;lt;T&amp;gt;&lt;/i&gt; base class for exactly this purpose and we can extend it like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5cfc2d16-0a65-4872-b026-c82df251e9a3" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DomainSignatureConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;AttributePropertyConvention&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;&amp;gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt; attribute, &lt;span style="color:#2b91af;"&gt;IPropertyInstance&lt;/span&gt; instance) { &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(instance.EntityType.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The DomainSignatureConvention tells fluent that all properties that are decorated with [DomainSignature] should form a unique key.&amp;nbsp; Now if I then define my entity like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d54e305c-c2cc-4f98-a45c-8a3cb5f7b64a" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Price&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Entity&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Security&lt;/span&gt; Security { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt; PriceDate { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color:#2b91af;"&gt;DomainSignature&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; CanadianPrice { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Bid { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Ask { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;decimal&lt;/span&gt; Close { &lt;span style="color:#0000ff;"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff;"&gt;set&lt;/span&gt;; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;then SchemaExport will generate DDL like this:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;div id="codeSnippet" style="border-style:none;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;table&lt;/span&gt; Prices (&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;    Id &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;not&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   PriceDate DATETIME,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   CanadianPrice &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   Bid &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   Ask &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   &lt;span style="color:#0000ff;"&gt;Close&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NUMERIC&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   SecurityFk &lt;span style="color:#0000ff;"&gt;INTEGER&lt;/span&gt;,&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;   &lt;span style="color:#0000ff;"&gt;primary&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;key&lt;/span&gt; (Id),&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;  &lt;span style="color:#0000ff;"&gt;unique&lt;/span&gt; (PriceDate, CanadianPrice)&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;)&lt;/pre&gt;
&amp;lt;!--CRLF--&amp;gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Except we have a small problem here.&amp;nbsp; The foreign key for the many-to-one relationship on &lt;i&gt;Security&lt;/i&gt; was not included in the &lt;i&gt;unique&lt;/i&gt; constraint.&amp;nbsp; To be quite honest I am not exactly sure why but I am guessing that the &lt;i&gt;AttributePropertyConvention &lt;/i&gt;does not work with a reference.&amp;nbsp; Instead I will need to add something to my &lt;i&gt;ReferenceConvention&lt;/i&gt; which is provided by default with s#arp architecture.&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2787227c-6879-4cc5-8c9a-f06d171b4095" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReferenceConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IReferenceConvention&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(FluentNHibernate.Conventions.Instances.&lt;span style="color:#2b91af;"&gt;IManyToOneInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Column(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Fk&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;.IsDefined(instance.Property, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;)))&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(&lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I don&amp;#39;t really like this as I don&amp;#39;t think the attribute should be ignored, but it does at least change the above to &lt;i&gt;unique(PriceDate, CanadianPrice, SecurityFk).&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;There are obviously many other types of constraints we could use, you could create an attribute for defining indexing on certain properties like [Indexable(&amp;quot;IndexName&amp;quot;)] and create a convention like this:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dc415913-baed-4310-ae51-951f5c4fcec1" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; _name;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; IndexableAttribute(&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name) { _name = name; }&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; GetName() { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _name; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IndexableConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;AttributePropertyConvention&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt;&amp;gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(&lt;span style="color:#2b91af;"&gt;IndexableAttribute&lt;/span&gt; attribute, &lt;span style="color:#2b91af;"&gt;IPropertyInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Index(attribute.GetName());&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now I can create indicies by using the [Indexable(&amp;quot;Name&amp;quot;)] attribute.&amp;nbsp; Properties with the same &amp;quot;Name&amp;quot; will be part of the same index constraint and indexed together.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I found it is actually a good idea to always index many-to-one relationships, something I briefly mentioned in a &lt;a href="http://www.lucisferre.net/post.aspx?id=ef779300-206b-4582-a9d0-4275b328c8c9"&gt;previous post&lt;/a&gt;.&amp;nbsp; Now that I am using fluent nhibernate however I can&amp;#39;t set the index property in the HBM files so I will need a convention for that.&amp;nbsp; It also makes sense to set this indexing on all many-to-one relationships so there is a better way to do this that does not involve the use of an attribute, instead we implement IRefrenceConvention.&amp;nbsp; S#arp architecture already includes this convention by default so we can just edit it:&lt;/p&gt;
&lt;div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0bc6d7e9-4abf-4c45-b26d-85fa8e16fca0" class="wlWriterEditableSmartContent" style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;"&gt;
&lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt;
&lt;div style="background:#fff;max-height:300px;overflow:auto;"&gt;&lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt;
&lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReferenceConvention&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IReferenceConvention&lt;/span&gt; {&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Apply(FluentNHibernate.Conventions.Instances.&lt;span style="color:#2b91af;"&gt;IManyToOneInstance&lt;/span&gt; instance) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Column(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Fk&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Attribute&lt;/span&gt;.IsDefined(instance.Property, &lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;DomainSignatureAttribute&lt;/span&gt;)))&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.UniqueKey(&lt;span style="color:#a31515;"&gt;&amp;quot;DomainSignature&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;else&lt;/span&gt; &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;instance.Index(instance.Property.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;);&lt;/li&gt;
&lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Notice the if.else, if we have already defined &lt;i&gt;UniqueKey&lt;/i&gt; defining Index would be redundant as unique key&amp;#39;s are already indexed.&amp;nbsp; When defining indexes you will typically see something like like the following DDL output from SchemaExport:&lt;/p&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre id="codeSnippet" style="border-style:none;margin:0em;padding:0px;overflow:visible;text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&amp;#39;Courier New&amp;#39;,courier,monospace;direction:ltr;color:black;font-size:8pt;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;index&lt;/span&gt; SecurityIndex &lt;span style="color:#0000ff;"&gt;on&lt;/span&gt; Prices (SecurityFk)&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;p&gt;It is pretty easy to customize the behavior of the fluent nhibernate AutoPersistenceModel using conventions and I find it can be very useful to have this type of fine grained control over your database schema generation.&lt;/p&gt;</description></item><item><title>CurrentSessionContext for Desktop development</title><link>http://nhforge.org/wikis/howtonh/currentsessioncontext-for-desktop-development/revision/0.aspx</link><pubDate>Fri, 06 Nov 2009 19:07:57 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:68</guid><dc:creator>Jason Meckley</dc:creator><description>Current revision posted to How to by Jason Meckley on 06/11/2009 04:07:57 p.m.&lt;br /&gt;
&lt;h2&gt;CurrentSessionContext for Desktop development&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Session&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IoC&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;container&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;CurrentSessionContext&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;I have used current session contexts in web applications with the ManagedWebContext class. I wanted to get this working in a non-asp.net environment though. And I wanted to keep the UI responsive, which means pushing as much as possible to the background.&amp;nbsp; I took a queue from Jeremy Miller&amp;#39;s article on &lt;a href="http://msdn.microsoft.com/en-us/magazine/ee309512.aspx"&gt;functional programming&lt;/a&gt;. To that end, here is one approach to managing Sessions in a threaded environment.&lt;/p&gt;
&lt;p&gt;we will utilize the typical MVP triad injecting our view into the presenter.&lt;/p&gt;
&lt;pre&gt;    public class Presenter : IPresenter
    {
        private readonly IView view;
        private readonly IService service;
        private readonly ICommandExecutor executor;

        public Presenter(IView view, IService service, ICommandExecutor executor)
        {
            this.view = view;
            this.service = service;
            this.executor = executor;
        }

        public virtual void UpdateView()
        {
            executor.Execute(() =&amp;gt;
                                 {
                                     var text = service.GetData(1);
                                     return () =&amp;gt; view.UpdateUserInterface(text);
                                 });
        }
    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;So what is the ICommandExecutor? This is where we centralize threading and session management. For SoC I have devided this into 2 implementations. one for threading the other for session management.&lt;/p&gt;
&lt;pre&gt;    public class AsynchronousExecutor : ICommandExecutor
    {
        private readonly SynchronizationContext synchronizationContext;
        private readonly ICommandExecutor executor;

        public AsynchronousExecutor(SynchronizationContext synchronizationContext, ICommandExecutor executor)
        {
            this.synchronizationContext = synchronizationContext;
            this.executor = executor;
        }

        public void Execute(Action action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(action));
        }

        public void Execute(Func&amp;lt;Action&amp;gt; action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(() =&amp;gt;
                        {
                           var continuation = action();
                           synchronizationContext.Send(x =&amp;gt; continuation(), null);
                        }));
        }
    }

    public class UnitOfWorkExecutor : ICommandExecutor
    {
        private readonly ISessionFactory factory;

        public UnitOfWorkExecutor(ISessionFactory factory)
        {
            this.factory = factory;
        }

        public void Execute(Action action)
        {
            ExecuteWithinAUnitOfWork(action);
        }

        public void Execute(Func&amp;lt;Action&amp;gt; action)
        {
            ExecuteWithinAUnitOfWork(() =&amp;gt; action());
        }

        private void ExecuteWithinAUnitOfWork(Action action)
        {
            try
            {
                using (var transaction = new TransactionScope())
                {
                    CurrentSessionContext.Bind(factory.OpenSession());
                    action();
                    transaction.Complete();
                }
            }
            finally
            {
                var session = CurrentSessionContext.Unbind(factory);
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }
    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;AsynchronousExecutor pushes work to the background while UnitOfWorkExecutor sets up the context of a session. For information on synchronizationContext I will defer you to Jeremy&amp;#39;s MSDN article. 2 steps left. 1) Configure NH and 2) wire this together.&lt;/p&gt;
&lt;p&gt;To configure NH we will use Fluent NHibernate. The important part is setting the CurrentSessionContext property. For those not using programmatic configuration, you would set the property in the hibernate.config file along with the other session factory configs.&lt;/p&gt;
&lt;p&gt;For wiring I&amp;#39;m using Castle Windsor. Any IoC should allow for something similar though. after placing the configuration and factory in the kernel we can use a factory method to resolve the session from the kernel. Note that the lifestyle of session is Transient. this is crucial, otherwise the sessions will not resolve correctly. We will use the same technique for resolving the SynchronizationContext as well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;    public class NHibernateFacility : AbstractFacility
    {
        protected override void Init()
        {
            var configuration = Fluently
                .Configure()
                .Database(() =&amp;gt; MsSqlConfiguration.MsSql2000)
                .Mappings(m =&amp;gt; m.FluentMappings.AddFromAssembly(GetType().Assembly))
                .ExposeConfiguration(ExtendConfiguration)
                .BuildConfiguration();

            Kernel.AddComponentInstance(&amp;quot;configuration&amp;quot;, configuration);
            Kernel.AddComponentInstance(&amp;quot;factory&amp;quot;, configuration.BuildSessionFactory());
            Kernel.Register(Component
                                .For&amp;lt;ISession&amp;gt;()
                                .LifeStyle.Is(LifestyleType.Transient)
                                .UsingFactoryMethod(k =&amp;gt; &lt;span style="text-decoration: line-through; color: red;"&gt;k.Resolve&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;ISessionFactory&amp;gt;().GetCurrentSession()));&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;k&lt;/span&gt;&lt;br /&gt;                                             &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Resolve&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;ISessionFactory&amp;gt;()&lt;/span&gt;&lt;br /&gt;                                             &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;GetCurrentSession()));&lt;/span&gt;
        }

        private static void ExtendConfiguration(Configuration cfg)
        {
            &lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;context&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;typeof(ThreadStaticSessionContext).AssemblyQualifiedName&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;           &amp;nbsp;cfg.SetProperty(Environment.CurrentSessionContextClass, &lt;span style="text-decoration: line-through; color: red;"&gt;typeof(ThreadStaticSessionContext).AssemblyQualifiedName)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;context)&lt;/span&gt;;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we wire up the entire container.&lt;/p&gt;
&lt;pre&gt;    internal static class Program
    {
        private static IWindsorContainer container;

        [STAThread]
        private static void Main()
        {
            container = new WindsorContainer()
                .AddFacility&amp;lt;FactorySupportFacility&amp;gt;()
                .AddFacility&amp;lt;NHibernateFacility&amp;gt;()
                .AddComponentLifeStyle&amp;lt;ICommandExecutor, AsynchronousExecutor&amp;gt;(LifestyleType.Singleton)
                .AddComponentLifeStyle&amp;lt;ICommandExecutor, UnitOfWorkExecutor&amp;gt;(LifestyleType.Singleton)
                .AddComponentLifeStyle&amp;lt;MainForm&amp;gt;(LifestyleType.Transient)
                .AddComponentLifeStyle&amp;lt;IPresenter, Presenter&amp;gt;(LifestyleType.Transient)
                .AddComponentLifeStyle&amp;lt;IService, Service&amp;gt;(LifestyleType.Transient)
                .Register(Component
                              .For&amp;lt;SynchronizationContext&amp;gt;()
                              .LifeStyle.Is(LifestyleType.Singleton)
                              .UsingFactoryMethod&amp;lt;SynchronizationContext&amp;gt;(CreateSynchronizationContext));

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(container.Resolve&amp;lt;MainForm&amp;gt;());
        }

        private static SynchronizationContext CreateSynchronizationContext()
        {
            if (SynchronizationContext.Current == null)
            {
                &lt;span style="text-decoration: line-through; color: red;"&gt;SynchronizationContext.SetSynchronizationContext(new&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;WindowsFormsSynchronizationContext())&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;context&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;WindowsFormsSynchronizationContext()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;br /&gt;               &amp;nbsp;&lt;span style="background: SpringGreen;"&gt;SynchronizationContext.SetSynchronizationContext(context)&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;            return SynchronizationContext.Current;&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;</description></item><item><title>CurrentSessionContext for Desktop development</title><link>http://nhforge.org/wikis/howtonh/currentsessioncontext-for-desktop-development/revision/3.aspx</link><pubDate>Fri, 06 Nov 2009 19:04:59 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:297</guid><dc:creator>Jason Meckley</dc:creator><description>Revision 3 posted to How to by Jason Meckley on 06/11/2009 04:04:59 p.m.&lt;br /&gt;
&lt;h2&gt;CurrentSessionContext for Desktop development&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, IoC container, CurrentSessionContext&lt;/div&gt;

&lt;p&gt;I have used current session contexts in web applications with the ManagedWebContext class. I wanted to get this working in a non-asp.net environment though. And I wanted to keep the UI responsive, which means pushing as much as possible to the background.&amp;nbsp; I took a queue from Jeremy Miller&amp;#39;s article on &lt;a href="http://msdn.microsoft.com/en-us/magazine/ee309512.aspx"&gt;functional programming&lt;/a&gt;. To that end, here is one approach to managing Sessions in a threaded environment.&lt;/p&gt;
&lt;p&gt;we will utilize the typical MVP triad injecting our view into the presenter.&lt;/p&gt;
&lt;pre&gt;    public class Presenter : IPresenter
    {
        private readonly IView view;
        private readonly IService service;
        private readonly ICommandExecutor executor;

        public Presenter(IView view, IService service, ICommandExecutor executor)
        {
            this.view = view;
            this.service = service;
            this.executor = executor;
        }

        public virtual void UpdateView()
        {
            executor.Execute(() =&amp;gt;
                                 {
                                     var text = service.GetData(1);
                                     return () =&amp;gt; view.UpdateUserInterface(text);
                                 });
        }
    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;So what is the ICommandExecutor? This is where we centralize threading and session management. For SoC I have devided this into 2 implementations. one for threading the other for session management.&lt;/p&gt;
&lt;pre&gt;    public class AsynchronousExecutor : ICommandExecutor
    {
        private readonly SynchronizationContext synchronizationContext;
        private readonly ICommandExecutor executor;

        public AsynchronousExecutor(SynchronizationContext synchronizationContext, ICommandExecutor executor)
        {
            this.synchronizationContext = synchronizationContext;
            this.executor = executor;
        }

        public void Execute(Action action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(action));
        }

        public void Execute(Func&amp;lt;Action&amp;gt; action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(() =&amp;gt;
                                     {
                                             var continuation = action();
                                            synchronizationContext.Send(x =&amp;gt; continuation(), null);
                                    }));
        }
    }

    public class UnitOfWorkExecutor : ICommandExecutor
    {
        private readonly ISessionFactory factory;

        public UnitOfWorkExecutor(ISessionFactory factory)
        {
            this.factory = factory;
        }

        public void Execute(Action action)
        {
            ExecuteWithinAUnitOfWork(action);
        }

        public void Execute(Func&amp;lt;Action&amp;gt; action)
        {
            ExecuteWithinAUnitOfWork(() =&amp;gt; action());
        }

        private void ExecuteWithinAUnitOfWork(Action action)
        {
            try
            {
                using (var transaction = new TransactionScope())
                {
                    CurrentSessionContext.Bind(factory.OpenSession());
                    action();
                    transaction.Complete();
                }
            }
            finally
            {
                var session = CurrentSessionContext.Unbind(factory);
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }
    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;AsynchronousExecutor pushes work to the background while UnitOfWorkExecutor sets up the context of a session. For information on synchronizationContext I will defer you to Jeremy&amp;#39;s MSDN article. 2 steps left. 1) Configure NH and 2) wire this together.&lt;/p&gt;
&lt;p&gt;To configure NH we will use Fluent NHibernate. The important part is setting the CurrentSessionContext property. For those not using programmatic configuration, you would set the property in the hibernate.config file along with the other session factory configs.&lt;/p&gt;
&lt;p&gt;For wiring I&amp;#39;m using Castle Windsor. Any IoC should allow for something similar though. after placing the configuration and factory in the kernel we can use a factory method to resolve the session from the kernel. Note that the lifestyle of session is Transient. this is crucial, otherwise the sessions will not resolve correctly. We will use the same technique for resolving the SynchronizationContext as well.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;    public class NHibernateFacility : AbstractFacility
    {
        protected override void Init()
        {
            var configuration = Fluently
                .Configure()
                .Database(() =&amp;gt; MsSqlConfiguration.MsSql2000)
                .Mappings(m =&amp;gt; m.FluentMappings.AddFromAssembly(GetType().Assembly))
                .ExposeConfiguration(ExtendConfiguration)
                .BuildConfiguration();

            Kernel.AddComponentInstance(&amp;quot;configuration&amp;quot;, configuration);
            Kernel.AddComponentInstance(&amp;quot;factory&amp;quot;, configuration.BuildSessionFactory());
            Kernel.Register(Component
                                .For&amp;lt;ISession&amp;gt;()
                                .LifeStyle.Is(LifestyleType.Transient)
                                .UsingFactoryMethod(k =&amp;gt; k.Resolve&amp;lt;ISessionFactory&amp;gt;().GetCurrentSession()));
        }

        private static void ExtendConfiguration(Configuration cfg)
        {
            cfg.SetProperty(Environment.CurrentSessionContextClass, typeof(ThreadStaticSessionContext).AssemblyQualifiedName);
        }
    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we wire up the entire container.&lt;/p&gt;
&lt;pre&gt;    internal static class Program
    {
        private static IWindsorContainer container;

        [STAThread]
        private static void Main()
        {
            container = new WindsorContainer()
                .AddFacility&amp;lt;FactorySupportFacility&amp;gt;()
                .AddFacility&amp;lt;NHibernateFacility&amp;gt;()
                .AddComponentLifeStyle&amp;lt;ICommandExecutor, AsynchronousExecutor&amp;gt;(LifestyleType.Singleton)
                .AddComponentLifeStyle&amp;lt;ICommandExecutor, UnitOfWorkExecutor&amp;gt;(LifestyleType.Singleton)
                .AddComponentLifeStyle&amp;lt;MainForm&amp;gt;(LifestyleType.Transient)
                .AddComponentLifeStyle&amp;lt;IPresenter, Presenter&amp;gt;(LifestyleType.Transient)
                .AddComponentLifeStyle&amp;lt;IService, Service&amp;gt;(LifestyleType.Transient)
                .Register(Component
                              .For&amp;lt;SynchronizationContext&amp;gt;()
                              .LifeStyle.Is(LifestyleType.Singleton)
                              .UsingFactoryMethod&amp;lt;SynchronizationContext&amp;gt;(CreateSynchronizationContext));

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(container.Resolve&amp;lt;MainForm&amp;gt;());
        }

        private static SynchronizationContext CreateSynchronizationContext()
        {
            if (SynchronizationContext.Current == null)
            {
                SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
            }
            return SynchronizationContext.Current;
        }
    }&lt;/pre&gt;</description></item><item><title>CurrentSessionContext for Desktop development</title><link>http://nhforge.org/wikis/howtonh/currentsessioncontext-for-desktop-development/revision/2.aspx</link><pubDate>Fri, 06 Nov 2009 19:03:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:296</guid><dc:creator>Jason Meckley</dc:creator><description>Revision 2 posted to How to by Jason Meckley on 06/11/2009 04:03:37 p.m.&lt;br /&gt;
&lt;h2&gt;CurrentSessionContext for Desktop development&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Session, IoC container, CurrentSessionContext&lt;/div&gt;

&lt;p&gt;I have used current session contexts in web applications with the ManagedWebContext class. I wanted to get this working in a non-asp.net environment though. And I wanted to keep the UI responsive, which means pushing as much as possible to the background.&amp;nbsp; I took a queue from Jeremy Miller&amp;#39;s article on &lt;a href="http://msdn.microsoft.com/en-us/magazine/ee309512.aspx"&gt;functional programming&lt;/a&gt;. To that end, here is one approach to managing Sessions in a threaded environment.&lt;/p&gt;
&lt;p&gt;we will utilize the typical MVP triad injecting our view into the presenter.&lt;/p&gt;
&lt;pre&gt;    public class Presenter : IPresenter
    {
        private readonly IView view;
        private readonly IService service;
        private readonly ICommandExecutor executor;

        public Presenter(IView view, IService service, ICommandExecutor executor)
        {
            this.view = view;
            this.service = service;
            this.executor = executor;
        }

        public virtual void UpdateView()
        {
            executor.Execute(() =&amp;gt;
                                 {
                                     var text = service.GetData(1);
                                     return () =&amp;gt; view.UpdateUserInterface(text);
                                 });
        }
    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;So what is the ICommandExecutor? This is where we centralize threading and session management. For SoC I have devided this into 2 implementations. one for threading the other for session management.&lt;/p&gt;
&lt;pre&gt;    public class AsynchronousExecutor : ICommandExecutor
    {
        private readonly SynchronizationContext synchronizationContext;
        private readonly ICommandExecutor executor;

        public AsynchronousExecutor(SynchronizationContext synchronizationContext, ICommandExecutor executor)
        {
            this.synchronizationContext = synchronizationContext;
            this.executor = executor;
        }

        public void Execute(Action action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(action));
        }

        public void Execute(Func&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;action&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;lt;Action&amp;gt;&lt;/span&gt; action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(() =&amp;gt;
                                                                     {
                                                                         var continuation = action();
                                                                         synchronizationContext.Send(x =&amp;gt; continuation(), null);
                                                                     }));
        }
    }

    public class UnitOfWorkExecutor : ICommandExecutor
    {
        private readonly ISessionFactory factory;

        public UnitOfWorkExecutor(ISessionFactory factory)
        {
            this.factory = factory;
        }

        public void Execute(Action action)
        {
            ExecuteWithinAUnitOfWork(action);
        }

        public void Execute(Func&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;action&amp;gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;lt;Action&amp;gt;&lt;/span&gt; action)
        {
            ExecuteWithinAUnitOfWork(() =&amp;gt; action());
        }

        private void ExecuteWithinAUnitOfWork(Action action)
        {
            try
            {
                using (var transaction = new TransactionScope())
                {
                    CurrentSessionContext.Bind(factory.OpenSession());
                    action();
                    transaction.Complete();
                }
            }
            finally
            {
                var session = CurrentSessionContext.Unbind(factory);
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }
    }&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/action&amp;gt;&amp;lt;/action&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;AsynchronousExecutor pushes work to the background while UnitOfWorkExecutor sets up the context of a session. For information on synchronizationContext I will defer you to Jeremy&amp;#39;s MSDN article. 2 steps left. 1) Configure NH and 2) wire this together.&lt;/p&gt;
&lt;p&gt;To configure NH we will use Fluent NHibernate. The important part is setting the CurrentSessionContext property. For those not using programmatic configuration, you would set the property in the hibernate.config file along with the other session factory configs.&lt;/p&gt;
&lt;p&gt;For wiring I&amp;#39;m using Castle Windsor. Any IoC should allow for something similar though. after placing the configuration and factory in the kernel we can use a factory method to resolve the session from the kernel. Note that the lifestyle of session is Transient. this is crucial, otherwise the sessions will not resolve correctly. We will use the same technique for resolving the SynchronizationContext as well.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;action&amp;gt;&amp;lt;action&amp;gt;&amp;lt;/action&amp;gt;&amp;lt;/action&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;    public class NHibernateFacility : AbstractFacility
    {
        protected override void Init()
        {
            var configuration = Fluently
                .Configure()
                .Database(() =&amp;gt; MsSqlConfiguration.MsSql2000)
                .Mappings(m =&amp;gt; m.FluentMappings.AddFromAssembly(GetType().Assembly))
                .ExposeConfiguration(ExtendConfiguration)
                .BuildConfiguration();

            Kernel.AddComponentInstance(&amp;quot;configuration&amp;quot;, configuration);
            Kernel.AddComponentInstance(&amp;quot;factory&amp;quot;, configuration.BuildSessionFactory());
            Kernel.Register(Component
                                .For&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;isession&amp;gt;()&lt;/span&gt;
                                &lt;span style="background: SpringGreen;"&gt;&amp;lt;ISession&amp;gt;()&lt;/span&gt;
                                .LifeStyle.Is(LifestyleType.Transient)
                                .UsingFactoryMethod(k =&amp;gt; k.Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;isessionfactory&amp;gt;().GetCurrentSession()));&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;&amp;lt;ISessionFactory&amp;gt;().GetCurrentSession()));&lt;/span&gt;
        }

        private static void ExtendConfiguration(Configuration cfg)
        {
            cfg.SetProperty(Environment.CurrentSessionContextClass, typeof(ThreadStaticSessionContext).AssemblyQualifiedName);
        }
    }&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/isessionfactory&amp;gt;&amp;lt;/isession&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;isession&amp;gt;&amp;lt;isessionfactory&amp;gt;Finally,&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Finally&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; we wire up the entire container.&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/isessionfactory&amp;gt;&amp;lt;/isession&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;isession&amp;gt;&amp;lt;isessionfactory&amp;gt;&amp;lt;/isessionfactory&amp;gt;&amp;lt;/isession&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;    &lt;span style="background: SpringGreen;"&gt;internal&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;static&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Program&lt;/span&gt;
    &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;static&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IWindsorContainer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;container&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;

        &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;STAThread]&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;static&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;void&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Main(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;
            &lt;span style="background: SpringGreen;"&gt;container&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;WindsorContainer(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AddFacility&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;FactorySupportFacility&amp;gt;()&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AddFacility&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;NHibernateFacility&amp;gt;()&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AddComponentLifeStyle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;ICommandExecutor,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;AsynchronousExecutor&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;(LifestyleType.Singleton)&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AddComponentLifeStyle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;ICommandExecutor,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;UnitOfWorkExecutor&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;(LifestyleType.Singleton)&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AddComponentLifeStyle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;MainForm&amp;gt;(LifestyleType.Transient)&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AddComponentLifeStyle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;IPresenter,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Presenter&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;(LifestyleType.Transient)&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;AddComponentLifeStyle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;IService,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Service&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;(LifestyleType.Transient)&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Register(Component&lt;/span&gt;
                              &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;SynchronizationContext&amp;gt;()&lt;/span&gt;
                              &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;LifeStyle.Is(LifestyleType.Singleton)&lt;/span&gt;
                              &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;UsingFactoryMethod&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;SynchronizationContext&amp;gt;(CreateSynchronizationContext));&lt;/span&gt;

            &lt;span style="background: SpringGreen;"&gt;Application.EnableVisualStyles()&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
            &lt;span style="background: SpringGreen;"&gt;Application.SetCompatibleTextRenderingDefault(false)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
            &lt;span style="background: SpringGreen;"&gt;Application.Run(container.Resolve&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;MainForm&amp;gt;());&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;

        &lt;span style="background: SpringGreen;"&gt;private&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;static&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SynchronizationContext&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;CreateSynchronizationContext(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;
            &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;SynchronizationContext.Current&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;null&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
            &lt;span style="background: SpringGreen;"&gt;{&lt;/span&gt;
                &lt;span style="background: SpringGreen;"&gt;SynchronizationContext.SetSynchronizationContext(new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;WindowsFormsSynchronizationContext())&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
            &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;
            &lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SynchronizationContext.Current&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
        &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;
    &lt;span style="background: SpringGreen;"&gt;}&lt;/span&gt;&lt;/pre&gt;</description></item><item><title>CurrentSessionContext for Desktop development</title><link>http://nhforge.org/wikis/howtonh/currentsessioncontext-for-desktop-development/revision/1.aspx</link><pubDate>Fri, 06 Nov 2009 18:56:45 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:295</guid><dc:creator>Jason Meckley</dc:creator><description>Revision 1 posted to How to by Jason Meckley on 06/11/2009 03:56:45 p.m.&lt;br /&gt;
&lt;p&gt;I have used current session contexts in web applications with the ManagedWebContext class. I wanted to get this working in a non-asp.net environment though. And I wanted to keep the UI responsive, which means pushing as much as possible to the background.&amp;nbsp; I took a queue from Jeremy Miller&amp;#39;s article on &lt;a href="http://msdn.microsoft.com/en-us/magazine/ee309512.aspx"&gt;functional programming&lt;/a&gt;. To that end, here is one approach to managing Sessions in a threaded environment.&lt;/p&gt;
&lt;p&gt;we will utilize the typical MVP triad injecting our view into the presenter.&lt;/p&gt;
&lt;pre&gt;    public class Presenter : IPresenter
    {
        private readonly IView view;
        private readonly IService service;
        private readonly ICommandExecutor executor;

        public Presenter(IView view, IService service, ICommandExecutor executor)
        {
            this.view = view;
            this.service = service;
            this.executor = executor;
        }

        public virtual void UpdateView()
        {
            executor.Execute(() =&amp;gt;
                                 {
                                     var text = service.GetData(1);
                                     return () =&amp;gt; view.UpdateUserInterface(text);
                                 });
        }
    }&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;So what is the ICommandExecutor? This is where we centralize threading and session management. For SoC I have devided this into 2 implementations. one for threading the other for session management.&lt;/p&gt;
&lt;pre&gt;    public class AsynchronousExecutor : ICommandExecutor
    {
        private readonly SynchronizationContext synchronizationContext;
        private readonly ICommandExecutor executor;

        public AsynchronousExecutor(SynchronizationContext synchronizationContext, ICommandExecutor executor)
        {
            this.synchronizationContext = synchronizationContext;
            this.executor = executor;
        }

        public void Execute(Action action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(action));
        }

        public void Execute(Func&amp;lt;action&amp;gt; action)
        {
            ThreadPool.QueueUserWorkItem(item =&amp;gt; executor.Execute(() =&amp;gt;
                                                                     {
                                                                         var continuation = action();
                                                                         synchronizationContext.Send(x =&amp;gt; continuation(), null);
                                                                     }));
        }
    }

    public class UnitOfWorkExecutor : ICommandExecutor
    {
        private readonly ISessionFactory factory;

        public UnitOfWorkExecutor(ISessionFactory factory)
        {
            this.factory = factory;
        }

        public void Execute(Action action)
        {
            ExecuteWithinAUnitOfWork(action);
        }

        public void Execute(Func&amp;lt;action&amp;gt; action)
        {
            ExecuteWithinAUnitOfWork(() =&amp;gt; action());
        }

        private void ExecuteWithinAUnitOfWork(Action action)
        {
            try
            {
                using (var transaction = new TransactionScope())
                {
                    CurrentSessionContext.Bind(factory.OpenSession());
                    action();
                    transaction.Complete();
                }
            }
            finally
            {
                var session = CurrentSessionContext.Unbind(factory);
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }
    }&lt;br /&gt;&amp;lt;/action&amp;gt;&amp;lt;/action&amp;gt;&lt;/pre&gt;
&lt;p&gt;AsynchronousExecutor pushes work to the background while UnitOfWorkExecutor sets up the context of a session. For information on synchronizationContext I will defer you to Jeremy&amp;#39;s MSDN article. 2 steps left. 1) Configure NH and 2) wire this together.&lt;/p&gt;
&lt;p&gt;To configure NH we will use Fluent NHibernate. The important part is setting the CurrentSessionContext property. For those not using programmatic configuration, you would set the property in the hibernate.config file along with the other session factory configs.&lt;/p&gt;
&lt;p&gt;For wiring I&amp;#39;m using Castle Windsor. Any IoC should allow for something similar though. after placing the configuration and factory in the kernel we can use a factory method to resolve the session from the kernel. Note that the lifestyle of session is Transient. this is crucial, otherwise the sessions will not resolve correctly. We will use the same technique for resolving the SynchronizationContext as well.&lt;/p&gt;
&lt;p&gt;&amp;lt;action&amp;gt;&amp;lt;action&amp;gt;&amp;lt;/action&amp;gt;&amp;lt;/action&amp;gt;&lt;/p&gt;
&lt;pre&gt;    public class NHibernateFacility : AbstractFacility
    {
        protected override void Init()
        {
            var configuration = Fluently
                .Configure()
                .Database(() =&amp;gt; MsSqlConfiguration.MsSql2000)
                .Mappings(m =&amp;gt; m.FluentMappings.AddFromAssembly(GetType().Assembly))
                .ExposeConfiguration(ExtendConfiguration)
                .BuildConfiguration();

            Kernel.AddComponentInstance(&amp;quot;configuration&amp;quot;, configuration);
            Kernel.AddComponentInstance(&amp;quot;factory&amp;quot;, configuration.BuildSessionFactory());
            Kernel.Register(Component
                                .For&amp;lt;isession&amp;gt;()
                                .LifeStyle.Is(LifestyleType.Transient)
                                .UsingFactoryMethod(k =&amp;gt; k.Resolve&amp;lt;isessionfactory&amp;gt;().GetCurrentSession()));
        }

        private static void ExtendConfiguration(Configuration cfg)
        {
            cfg.SetProperty(Environment.CurrentSessionContextClass, typeof(ThreadStaticSessionContext).AssemblyQualifiedName);
        }
    }&lt;br /&gt;&amp;lt;/isessionfactory&amp;gt;&amp;lt;/isession&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;lt;isession&amp;gt;&amp;lt;isessionfactory&amp;gt;Finally, we wire up the entire container.&amp;lt;/isessionfactory&amp;gt;&amp;lt;/isession&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt;isession&amp;gt;&amp;lt;isessionfactory&amp;gt;&amp;lt;/isessionfactory&amp;gt;&amp;lt;/isession&amp;gt;&lt;/p&gt;</description></item><item><title>NHibernate Property Validator for ASP.NET</title><link>http://nhforge.org/wikis/howtonh/nhibernate-property-validator-for-asp-net/revision/0.aspx</link><pubDate>Fri, 09 Oct 2009 11:51:45 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:67</guid><dc:creator>Ricardo Peres</dc:creator><description>Current revision posted to How to by Ricardo Peres on 09/10/2009 08:51:45 a.m.&lt;br /&gt;
&lt;p&gt;
&lt;p&gt;Suppose you want to validate an ASP.NET&amp;nbsp;control based on some validation attributes from a property. You may know that the Enterprise Library Application Block&amp;#39;s&amp;nbsp;&lt;strong&gt;PropertyProxyValidator&lt;/strong&gt;&amp;nbsp;does just this, the problem is that it uses Enterprise Library&amp;#39;s own validation attributes. If you want to use NHibernate Validator&amp;#39;s, your on your own.&lt;/p&gt;
&lt;p&gt;I have written a small validator control, based on Enterprise Library&amp;#39;s, that may come in handy. Here is the code:&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;NHibernatePropertyValidator&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;:&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;BaseValidator&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;ValidationSummaryDisplayMode&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;displayMode;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;propertyName;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;sourceTypeName;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;override&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;OnInit(&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;EventArgs&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;e)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;((&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Enabled ==&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;) &amp;amp;&amp;amp; (&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Visible ==&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;))&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Page.RegisterRequiresControlState(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.OnInit(e);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;override&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;LoadControlState(&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;savedState)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;[] state = savedState&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;as&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;[];&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.LoadControlState(state[0]);&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.displayMode = (&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;ValidationSummaryDisplayMode&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;) state [ 1 ];&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.PropertyName = (&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;) state [ 2 ];&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.SourceTypeName = (&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;) state [ 3 ];&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;override&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;SaveControlState()&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;[] state =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;[ 4 ];&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;state [ 0 ] =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;base&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.SaveControlState();&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;state [ 1 ] =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.DisplayMode;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;state [ 2 ] =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.PropertyName;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;state [ 3 ] =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.SourceTypeName;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(state);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;override&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Boolean&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;EvaluateIsValid()&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;type =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Type&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.GetType(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.SourceTypeName,&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;);&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(type !=&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;PropertyInfo&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;prop = type.GetProperty(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.PropertyName,&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;BindingFlags&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Instance |&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;BindingFlags&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Public |&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;BindingFlags&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.GetProperty);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(prop !=&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;IClassValidator&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;validator =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;ClassValidator&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;(type);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;obj =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Activator&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.CreateInstance(type);&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Object&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;value =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.GetControlValidationValue(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.ControlToValidate);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;prop.SetValue(obj,&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Convert&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.ChangeType(value, prop.PropertyType),&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;);&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;InvalidValue&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;[] results = validator.GetInvalidValues(obj,&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.PropertyName);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.ErrorMessage =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.formatErrorMessage(results);&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(results.Length == 0);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.ErrorMessage =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Empty;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;formatErrorMessage(&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;InvalidValue&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;[] results)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;str =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Empty;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;str2 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Empty;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;str3 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Empty;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;str4 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Empty;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;StringBuilder&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;builder =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;StringBuilder&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;();&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; switch&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.DisplayMode)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;ValidationSummaryDisplayMode&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.List:&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;str3 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;ValidationSummaryDisplayMode&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.SingleParagraph:&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;str3 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;str4 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;:&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;str =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;lt;ul&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;str2 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;lt;li&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;str3 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;lt;/li&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;str4 =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;lt;/ul&amp;gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(results.Length != 0)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Append(str);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;foreach&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;InvalidValue&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;result&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;in&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;results)&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Append(str2);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Append(result.Message);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Append(str3);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; builder.Append(str4);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(builder.ToString());&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [Browsable(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;)]&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [DefaultValue(&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;ValidationSummaryDisplayMode&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.List)]&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;ValidationSummaryDisplayMode&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;DisplayMode&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.displayMode);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;set&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.displayMode =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [Browsable(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;)]&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;DefaultValue&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;(&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;)]&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;PropertyName&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.propertyName);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;set&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.propertyName =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;??&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Empty;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;Browsable&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;)]&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;
&lt;p&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;DefaultValue&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;(&lt;/span&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&lt;span style="color:#a31515;font-size:x-small;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;)]&lt;/span&gt;&lt;/p&gt;
&lt;span style="font-size:x-small;"&gt;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;SourceTypeName&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;(&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.sourceTypeName);&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/span&gt;
&lt;p&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;set&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#000000;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.sourceTypeName =&amp;nbsp;&lt;/span&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;&lt;span style="color:#0000ff;font-size:x-small;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&amp;nbsp;??&amp;nbsp;&lt;/span&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;&lt;span style="color:#2b91af;font-size:x-small;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;.Empty;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Basically, it uses the NHibernate Validator&amp;#39;s&amp;nbsp;&lt;strong&gt;ClassValidator&lt;/strong&gt;&amp;nbsp;to do the actual validation.&lt;/p&gt;
&lt;p&gt;This is how you would use it:&lt;/p&gt;
&lt;p&gt;&amp;lt;asp:TextBox runat=&amp;quot;server&amp;quot; ID=&amp;quot;email&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;xxx:NHibernatePropertyValidator runat=&amp;quot;server&amp;quot; ControlToValidate=&amp;quot;email&amp;quot; SourceTypeName=&amp;quot;SomeClass, SomeAssembly&amp;quot; PropertyName=&amp;quot;Email&amp;quot;/&amp;gt;&lt;/p&gt;
&lt;p&gt;If you combine it with Web Client Software Factory&amp;#39;s&amp;nbsp;&lt;strong&gt;ServerSideValidationExtnder&lt;/strong&gt;, you can even have client-side (AJAX) validation. Give it a try!&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Simple audit metadata and logical deletion of business entities</title><link>http://nhforge.org/wikis/howtonh/simple-audit-metadata-and-logical-deletion-of-business-entities/revision/0.aspx</link><pubDate>Sun, 21 Jun 2009 00:44:39 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:63</guid><dc:creator>Raul Carlomagno</dc:creator><description>Current revision posted to How to by Raul Carlomagno on 20/06/2009 09:44:39 p.m.&lt;br /&gt;
&lt;h2&gt;Simple audit metadata and logical deletion of business entities&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Audit&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;logical&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;delete&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listener&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;filter&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;This article is just about a simple way to achieve auditing and perform logical delete of your sensible data, doing use of the listeners features of NHibernate. Design patterns and &lt;span style="text-decoration: line-through; color: red;"&gt;arquitecture&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;architecture&lt;/span&gt; principles are outside of the scope of the article, this is just an example, take it as a guide.&lt;/p&gt;
&lt;p&gt;Remember the old days when you had to audit data stored on your database, every time you build a query to insert, update or delete data, you had to &amp;ldquo;remember&amp;rdquo; to include the values for auditing (timestamp, user id). It was worse if you had stored procedures (by the way, i hate them) because suppose that was needed to take user credentials from another service to audit, you were limited by Transact-SQL language inside the stored procedure to access that service. Anyway, every time you build a query, you need those metadata values to store them, it could be good if you can centralize the place where you obtain and set those values (remember DRY).&lt;/p&gt;
&lt;p&gt;Maybe, in a strict way, auditing would take place in the business layer, but we are not going to miss this feature that NHibernate has for us, so auditing will take place in the data access layer. With NHibernate we can override its native methods when performing data access, NHibernate will fire events which we will have to override to audit the data and of course to do a logical deletion of our entities.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s get inside the code and explain the domain. In the example we have a BaseEntity for any persistible entity, and then, for the auditable entities, the AuditableEntity which inherits from BaseEntity. We want to audit the cars inside the database. We will audit the date and time, the user, the IP address and the hostname for each action on the entity (insert, update, delete), i think this auditing metada is fine, you can add more of course, your fantasy is your limit.&lt;/p&gt;
&lt;p&gt;In fact this can&amp;rsquo;t be called audit, because we are not tracking changes of entities, just who did an action, when and from, pseudoaudit? [:D]&lt;/p&gt;
&lt;p&gt;&lt;a href="/wikis/howtonh/add.aspx/$image24.png"&gt;&lt;img title="image" style="display:inline;border-width:0px;border:0;" alt="image" src="http://i40.tinypic.com/16iijs.png" width="407" border="0" height="712" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;So, the example solution has a test fixture to test the purpose of the example. It was developed with NHibernate 2.1.0.2001.&lt;/p&gt;
&lt;p&gt;No more words, show me the code.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see the mapping files.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Car.hbm.xml&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:411b4d59-2c28-4172-ab11-be3aabfc1650" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;br /&gt;                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;br /&gt;                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Car&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;cars&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Int64&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;hilo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;max_lo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;50&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;param&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;BrandName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;brand_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModelName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;model_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt;audit attributes&lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;br /&gt;        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;added_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;br /&gt;        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;modified_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;br /&gt;        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; condition&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_date IS NULL&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter-def &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;It has the Id, 3 owned properties, and 9 properties for metadata auditing. The filter part is about logical deletes, we are going to talk about it in later. &lt;/p&gt;
&lt;p&gt;The &lt;b&gt;User.hbm.xml &lt;/b&gt;is really simple, we are not going to show it, it has no sense, anyway, the example solution can be downloaded.&lt;/p&gt;
&lt;p&gt;Now it&amp;rsquo;s time to show the NHibernate configuration file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;hibernate.cfg.xml&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3ba95b40-497a-4b28-8b49-14fc1742bf02" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration  &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      Server=(local)\SQLEXPRESS;initial catalog=auditexample;Integrated Security=SSPI&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;adonet.batch_size&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;10&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;false&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;query.substitutions&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true 1, false 0, yes &amp;#39;Y&amp;#39;, no &amp;#39;N&amp;#39;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomSaveUpdateEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;br /&gt;        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;save-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomDeleteEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;br /&gt;        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;delete&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s analyze it, in the configuration file there is an attribute that is not so common, the listener attribute, as we mention before, with these attributes we are forcing NHibernate to lookup for the custom event listener that we inherited from the default listeners for ISession.Delete() and ISession.SaveUpdate() methods. Inside both class CustomSaveUpdateEventListener and CustomDeleteEventListener will appear a class called SecurityContext which handles the metada for auditing, this class will be discused later, after explaining both listeners.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;CustomSaveUpdateEventListener.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e2d0d747-e26f-4d1c-a83b-59a459662651" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; CustomSaveUpdateEventListener : DefaultSaveOrUpdateEventListener&lt;br /&gt;{&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;override&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; PerformSaveOrUpdate(SaveOrUpdateEvent evt)&lt;br /&gt;   {&lt;br /&gt;       var entity &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; evt.Entity &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditableEntity;&lt;br /&gt;&lt;br /&gt;          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (entity &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;){&lt;br /&gt;              &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (entity.Id &lt;/span&gt;&lt;span style="color:#000000;"&gt;==&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;br /&gt;                  ProcessEntityForInserting(entity);&lt;br /&gt;              &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;                  ProcessEntityForUpdating(entity);&lt;br /&gt;          }&lt;br /&gt;       &lt;br /&gt;          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;base&lt;/span&gt;&lt;span style="color:#000000;"&gt;.PerformSaveOrUpdate(evt);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;virtual&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForInserting(AuditableEntity entity)&lt;br /&gt;    {&lt;br /&gt;       entity.AddedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;&lt;br /&gt;       entity.AddedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       entity.AddedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();&lt;br /&gt;       entity.AddedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;virtual&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForUpdating(AuditableEntity entity)&lt;br /&gt;    {&lt;br /&gt;       entity.ModifiedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;&lt;br /&gt;       entity.ModifiedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       entity.ModifiedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();&lt;br /&gt;       entity.ModifiedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;CustomDeleteEventListener.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0d9f2bf9-3a0b-4402-832d-e0e844f30d37" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; CustomDeleteEventListener : DefaultDeleteEventListener&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;override&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; DeleteEntity(IEventSource session, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; entity,&lt;br /&gt;                        NHibernate.Engine.EntityEntry entityEntry,&lt;br /&gt;                        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; isCascadeDeleteEnabled,&lt;br /&gt;                        NHibernate.Persister.Entity.IEntityPersister persister,&lt;br /&gt;                        Iesi.Collections.ISet transientEntities)&lt;br /&gt;   {&lt;br /&gt;        var auditableEntity &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; entity &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditableEntity;&lt;br /&gt;&lt;br /&gt;           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (auditableEntity &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;){&lt;br /&gt;              ProcessEntityForLogicalDelete(auditableEntity);&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.CascadeBeforeDelete(session, persister, entity,&lt;br /&gt;                   entityEntry, transientEntities);&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.CascadeAfterDelete(session, persister, entity,&lt;br /&gt;                   transientEntities);&lt;br /&gt;       } &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt; {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;normal delete&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;base&lt;/span&gt;&lt;span style="color:#000000;"&gt;.DeleteEntity(session, entity, entityEntry,&lt;br /&gt;                isCascadeDeleteEnabled, persister, transientEntities);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForLogicalDelete(AuditableEntity entity)&lt;br /&gt;   {&lt;br /&gt;        entity.ErasedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;&lt;br /&gt;        entity.ErasedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;        entity.ErasedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();&lt;br /&gt;        entity.ErasedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();&lt;br /&gt;   }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;SecurityContext.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:64c46d35-180d-4eea-b740-092564076a3d" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; Ridiculous simple crosscut security class to get user credentials and audit metadata&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext&lt;br /&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;maybe System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(), you decide&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; User LoggedUser {&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;set&lt;/span&gt;&lt;span style="color:#000000;"&gt;;}&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetHostName()&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dns.GetHostName();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetHostIP()&lt;br /&gt;   {&lt;br /&gt;        IPAddress[] addresses &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dns.GetHostAddresses(Dns.GetHostName());&lt;br /&gt;        &lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;be carefull and filter the public ip, use the local one&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;and of course, this has no sense for an httpcontext, use remote host ip instead&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (addresses.Length &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)&lt;br /&gt;            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; addresses[&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;].ToString();&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;   }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;This class is just a simple helper to get the metadata needed for auditing, in this example it holds the session of the logged user too.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a58b502c-190d-4d18-9988-4eed1524b883" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;[TestFixture]&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditTest&lt;br /&gt;{&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; Configuration _configuration;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; ISessionFactory _sessionFactory;&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; User _sampleUser1;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; User _sampleUser2;&lt;br /&gt;&lt;br /&gt;   [TestFixtureSetUp]&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestFixtureSetUp()&lt;br /&gt;   {&lt;br /&gt;       XmlConfigurator.Configure();&lt;br /&gt;&lt;br /&gt;       _configuration &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Configuration();&lt;br /&gt;       _configuration.Configure();&lt;br /&gt;       _sessionFactory &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _configuration.BuildSessionFactory();&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;drop all tables on database&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;       _sessionFactory.OpenSession().CreateSQLQuery(&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;IF OBJECT_ID (N&amp;#39;dbo.cars&amp;#39;, N&amp;#39;U&amp;#39;) IS NOT NULL DELETE cars&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        ).ExecuteUpdate();&lt;br /&gt;       &lt;br /&gt;       _sessionFactory.OpenSession().CreateSQLQuery(&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;IF OBJECT_ID (N&amp;#39;dbo.users&amp;#39;, N&amp;#39;U&amp;#39;) IS NOT NULL DELETE users&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        ).ExecuteUpdate();&lt;br /&gt;       &lt;br /&gt;       _sessionFactory.OpenSession().CreateSQLQuery(&lt;br /&gt;               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;EXEC sp_MSforeachtable ?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        ).SetParameter(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;DROP TABLE ?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).ExecuteUpdate();&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; SchemaExport(_configuration).Create(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;br /&gt;&lt;br /&gt;       PrepareLoggedUsers();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [TestFixtureTearDown]&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestFixtureTearDown()&lt;br /&gt;   {&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;new SchemaExport(_configuration).Drop(false, true);&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;       _sessionFactory.Close();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [SetUp]&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestSetUp()&lt;br /&gt;   {&lt;br /&gt;       _sessionFactory.OpenSession().CreateQuery(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;delete Car&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).ExecuteUpdate();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; PrepareLoggedUsers()&lt;br /&gt;   {&lt;br /&gt;       _sampleUser1 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; User() { Login &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;rcarlomagno&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Raul&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;br /&gt;               LastName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Carlomagno&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Password &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;jijiji&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; };&lt;br /&gt;       _sampleUser2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; User() { Login &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;ndaponte&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Nadia&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;br /&gt;               LastName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Daponte&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Password &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;jujujuju&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; };&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())&lt;br /&gt;       {&lt;br /&gt;           session.Save(_sampleUser1);&lt;br /&gt;           session.Save(_sampleUser2);&lt;br /&gt;           transaction.Commit();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sampleUser1;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [Test]&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditAddedEntity()&lt;br /&gt;   {&lt;br /&gt;       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Renault&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Clio&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;br /&gt;               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;110000&lt;/span&gt;&lt;span style="color:#000000;"&gt; };&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())&lt;br /&gt;       {&lt;br /&gt;           session.SaveOrUpdate(car);&lt;br /&gt;           transaction.Commit();&lt;br /&gt;           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);&lt;br /&gt;&lt;br /&gt;       Assert.IsNotNull(car);&lt;br /&gt;       Assert.IsNotNull(car.AddedDate);&lt;br /&gt;       Assert.IsNotNull(car.AddedBy);&lt;br /&gt;       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.AddedFromHostName));&lt;br /&gt;       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.AddedFromIP));&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [Test]&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditModifiedEntity()&lt;br /&gt;   {&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ford&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ka&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;97544&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;&lt;br /&gt;       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName,&lt;br /&gt;               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers };&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())&lt;br /&gt;       {&lt;br /&gt;           session.SaveOrUpdate(car);&lt;br /&gt;           transaction.Commit();&lt;br /&gt;           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);&lt;br /&gt;&lt;br /&gt;       Assert.IsNotNull(car);&lt;br /&gt;       Assert.IsNotNull(car.AddedDate);&lt;br /&gt;       Assert.IsNotNull(car.AddedBy);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;let&amp;#39;s change logged user and wait 2 seconds to change the car&amp;#39;s data&lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;       SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sampleUser2;&lt;br /&gt;       Thread.Sleep(&lt;/span&gt;&lt;span style="color:#800080;"&gt;2000&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;br /&gt;&lt;br /&gt;       car.Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;54000&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       car.ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Fiesta&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())&lt;br /&gt;       {&lt;br /&gt;           session.SaveOrUpdate(car);&lt;br /&gt;           transaction.Commit();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);&lt;br /&gt;&lt;br /&gt;       Assert.IsNotNull(car);&lt;br /&gt;       Assert.IsNotNull(car.ModifiedBy);&lt;br /&gt;       Assert.IsNotNull(car.ModifiedDate);&lt;br /&gt;       Assert.AreNotEqual(car.ModifiedDate, car.AddedDate);&lt;br /&gt;       Assert.AreNotEqual(car.ModifiedBy, car.AddedBy);&lt;br /&gt;       Assert.AreNotEqual(car.Kilometers, kilometers);&lt;br /&gt;       Assert.AreNotEqual(car.ModelName, modelName);&lt;br /&gt;       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ModifiedFromHostName));&lt;br /&gt;       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ModifiedFromIP));&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   [Test]&lt;br /&gt;   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditErasedEntity()&lt;br /&gt;   {&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Citroen&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;C3&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;74665&lt;/span&gt;&lt;span style="color:#000000;"&gt;;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;&lt;br /&gt;&lt;br /&gt;       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName,&lt;br /&gt;               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers };&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())&lt;br /&gt;       {&lt;br /&gt;           session.SaveOrUpdate(car);&lt;br /&gt;           transaction.Commit();&lt;br /&gt;           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())&lt;br /&gt;       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())&lt;br /&gt;       {&lt;br /&gt;           session.Delete(car);&lt;br /&gt;           transaction.Commit();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);&lt;br /&gt;&lt;br /&gt;       Assert.IsNotNull(car);&lt;br /&gt;       Assert.IsNotNull(car.ErasedBy);&lt;br /&gt;       Assert.IsNotNull(car.ErasedDate);&lt;br /&gt;       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ErasedFromHostName));&lt;br /&gt;       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ErasedFromIP));&lt;br /&gt;&lt;br /&gt;       ISession tmpSession &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession();&lt;br /&gt;       tmpSession.EnableFilter(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;avoidLogicalDeleted&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;br /&gt;       Assert.AreEqual(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, tmpSession.CreateCriteria&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;().List().Count);&lt;br /&gt;       tmpSession.DisableFilter(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;avoidLogicalDeleted&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);&lt;br /&gt;       Assert.AreEqual(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, tmpSession.CreateCriteria&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;().List().Count);&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;This test fixture proves that i am not lying. :-)&lt;/p&gt;
&lt;p&gt;A few last words, in CanAuditErasedEntity() method, at the end of it, it is supposed that if we deleted logically an entity, we don&amp;rsquo;t want to see it in the results, so before doing a Criteria or HQL to get some data, we must enable the filter on the ISession, because filters are disabled by default. I do not know if there is a way to enable it on the ISessionFactory, to avoid manually enable it each time we want to get some data.&lt;/p&gt;
&lt;p&gt;Well, this is the first time i write an article, i hope the next will be better. Sorry for my english.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.2shared.com/file/6380003/67d562c8/AuditExample.html"&gt;Solution Download&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Simple audit metadata and logical deletion of business entities</title><link>http://nhforge.org/wikis/howtonh/simple-audit-metadata-and-logical-deletion-of-business-entities/revision/3.aspx</link><pubDate>Sat, 20 Jun 2009 05:56:26 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:288</guid><dc:creator>Raul Carlomagno</dc:creator><description>Revision 3 posted to How to by Raul Carlomagno on 20/06/2009 02:56:26 a.m.&lt;br /&gt;
&lt;h2&gt;Simple audit metadata and logical deletion of business entities&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, logical delete, listener, filter&lt;/div&gt;

&lt;p&gt;This article is just about a simple way to achieve auditing and perform logical delete of your sensible data, doing use of the listeners features of NHibernate. Design patterns and arquitecture principles are outside of the scope of the article, this is just an example, take it as a guide.&lt;/p&gt;
&lt;p&gt;Remember the old days when you had to audit data stored on your database, every time you build a query to insert, update or delete data, you had to &amp;ldquo;remember&amp;rdquo; to include the values for auditing (timestamp, user id). It was worse if you had stored procedures (by the way, i hate them) because suppose that was needed to take user credentials from another service to audit, you were limited by Transact-SQL language inside the stored procedure to access that service. Anyway, every time you build a query, you need those metadata values to store them, it could be good if you can centralize the place where you obtain and set those values (remember DRY).&lt;/p&gt;
&lt;p&gt;Maybe, in a strict way, auditing would take place in the business layer, but we are not going to miss this feature that NHibernate has for us, so auditing will take place in the data access layer. With NHibernate we can override its native methods when performing data access, NHibernate will fire events which we will have to override to audit the data and of course to do a logical deletion of our entities.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s get inside the code and explain the domain. In the example we have a BaseEntity for any persistible entity, and then, for the auditable entities, the AuditableEntity which inherits from BaseEntity. We want to audit the cars inside the database. We will audit the date and time, the user, the IP address and the hostname for each action on the entity (insert, update, delete), i think this auditing metada is fine, you can add more of course, your fantasy is your limit.&lt;/p&gt;
&lt;p&gt;In fact this can&amp;rsquo;t be called audit, because we are not tracking changes of entities, just who did an action, when and from, pseudoaudit? [:D]&lt;/p&gt;
&lt;p&gt;&lt;a href="/wikis/howtonh/add.aspx/$image24.png"&gt;&lt;img title="image" style="display:inline;border-width:0px;border:0;" alt="image" src="http://i40.tinypic.com/16iijs.png" width="407" border="0" height="712" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;So, the example solution has a test fixture to test the purpose of the example. It was developed with NHibernate 2.1.0.2001.&lt;/p&gt;
&lt;p&gt;No more words, show me the code.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see the mapping files.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Car.hbm.xml&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:411b4d59-2c28-4172-ab11-be3aabfc1650" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Car&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;cars&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Int64&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;hilo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;max_lo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;50&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;param&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;BrandName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;brand_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModelName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;model_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt;audit attributes&lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;added_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;modified_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; condition&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_date IS NULL&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter-def &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;It has the Id, 3 owned properties, and 9 properties for metadata auditing. The filter part is about logical deletes, we are going to talk about it in later. &lt;/p&gt;
&lt;p&gt;The &lt;b&gt;User.hbm.xml &lt;/b&gt;is really simple, we are not going to show it, it has no sense, anyway, the example solution can be downloaded.&lt;/p&gt;
&lt;p&gt;Now it&amp;rsquo;s time to show the NHibernate configuration file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;hibernate.cfg.xml&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3ba95b40-497a-4b28-8b49-14fc1742bf02" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration  &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      Server=(local)\SQLEXPRESS;initial catalog=auditexample;Integrated Security=SSPI
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;adonet.batch_size&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;10&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;false&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;query.substitutions&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true 1, false 0, yes &amp;#39;Y&amp;#39;, no &amp;#39;N&amp;#39;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomSaveUpdateEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;save-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomDeleteEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;delete&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s analyze it, in the configuration file there is an attribute that is not so common, the listener attribute, as we mention before, with these attributes we are forcing NHibernate to lookup for the custom event listener that we inherited from the default listeners for ISession.Delete() and ISession.SaveUpdate() methods. Inside both class CustomSaveUpdateEventListener and CustomDeleteEventListener will appear a class called SecurityContext which handles the metada for auditing, this class will be discused later, after explaining both listeners.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;CustomSaveUpdateEventListener.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e2d0d747-e26f-4d1c-a83b-59a459662651" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; CustomSaveUpdateEventListener : DefaultSaveOrUpdateEventListener
{
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;override&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; PerformSaveOrUpdate(SaveOrUpdateEvent evt)
   {
       var entity &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; evt.Entity &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditableEntity;

          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (entity &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;){
              &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (entity.Id &lt;/span&gt;&lt;span style="color:#000000;"&gt;==&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
                  ProcessEntityForInserting(entity);
              &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;
                  ProcessEntityForUpdating(entity);
          }
       
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;base&lt;/span&gt;&lt;span style="color:#000000;"&gt;.PerformSaveOrUpdate(evt);
   }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;virtual&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForInserting(AuditableEntity entity)
    {
       entity.AddedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;
       entity.AddedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       entity.AddedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();
       entity.AddedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();
    }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;virtual&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForUpdating(AuditableEntity entity)
    {
       entity.ModifiedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;
       entity.ModifiedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       entity.ModifiedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();
       entity.ModifiedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();
    }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;CustomDeleteEventListener.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0d9f2bf9-3a0b-4402-832d-e0e844f30d37" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; CustomDeleteEventListener : DefaultDeleteEventListener
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;override&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; DeleteEntity(IEventSource session, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; entity,
                        NHibernate.Engine.EntityEntry entityEntry,
                        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; isCascadeDeleteEnabled,
                        NHibernate.Persister.Entity.IEntityPersister persister,
                        Iesi.Collections.ISet transientEntities)
   {
        var auditableEntity &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; entity &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditableEntity;

           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (auditableEntity &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;){
              ProcessEntityForLogicalDelete(auditableEntity);
               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.CascadeBeforeDelete(session, persister, entity,
                   entityEntry, transientEntities);
               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.CascadeAfterDelete(session, persister, entity,
                   transientEntities);
       } &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt; {
            &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;normal delete&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;base&lt;/span&gt;&lt;span style="color:#000000;"&gt;.DeleteEntity(session, entity, entityEntry,
                isCascadeDeleteEnabled, persister, transientEntities);
       }
   }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForLogicalDelete(AuditableEntity entity)
   {
        entity.ErasedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;
        entity.ErasedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
        entity.ErasedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();
        entity.ErasedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();
   }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;SecurityContext.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:64c46d35-180d-4eea-b740-092564076a3d" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; Ridiculous simple crosscut security class to get user credentials and audit metadata
&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext
{
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;maybe System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(), you decide&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; User LoggedUser {&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;set&lt;/span&gt;&lt;span style="color:#000000;"&gt;;}

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetHostName()
    {
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dns.GetHostName();
    }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetHostIP()
   {
        IPAddress[] addresses &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dns.GetHostAddresses(Dns.GetHostName());
        
        &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;be carefull and filter the public ip, use the local one
        &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;and of course, this has no sense for an httpcontext, use remote host ip instead&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (addresses.Length &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; addresses[&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;].ToString();
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
   }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;This class is just a simple helper to get the metadata needed for auditing, in this example it holds the session of the logged user too.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a58b502c-190d-4d18-9988-4eed1524b883" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;[TestFixture]
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditTest
{
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; Configuration _configuration;
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; ISessionFactory _sessionFactory;

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; User _sampleUser1;
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; User _sampleUser2;

   [TestFixtureSetUp]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestFixtureSetUp()
   {
       XmlConfigurator.Configure();

       _configuration &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Configuration();
       _configuration.Configure();
       _sessionFactory &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _configuration.BuildSessionFactory();

       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;drop all tables on database&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;       _sessionFactory.OpenSession().CreateSQLQuery(
               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;IF OBJECT_ID (N&amp;#39;dbo.cars&amp;#39;, N&amp;#39;U&amp;#39;) IS NOT NULL DELETE cars&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        ).ExecuteUpdate();
       
       _sessionFactory.OpenSession().CreateSQLQuery(
               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;IF OBJECT_ID (N&amp;#39;dbo.users&amp;#39;, N&amp;#39;U&amp;#39;) IS NOT NULL DELETE users&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        ).ExecuteUpdate();
       
       _sessionFactory.OpenSession().CreateSQLQuery(
               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;EXEC sp_MSforeachtable ?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        ).SetParameter(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;DROP TABLE ?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).ExecuteUpdate();

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; SchemaExport(_configuration).Create(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;);

       PrepareLoggedUsers();
   }

   [TestFixtureTearDown]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestFixtureTearDown()
   {
       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;new SchemaExport(_configuration).Drop(false, true);&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;       _sessionFactory.Close();
   }

   [SetUp]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestSetUp()
   {
       _sessionFactory.OpenSession().CreateQuery(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;delete Car&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).ExecuteUpdate();
   }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; PrepareLoggedUsers()
   {
       _sampleUser1 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; User() { Login &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;rcarlomagno&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Raul&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
               LastName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Carlomagno&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Password &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;jijiji&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; };
       _sampleUser2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; User() { Login &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;ndaponte&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Nadia&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
               LastName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Daponte&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Password &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;jujujuju&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; };

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.Save(_sampleUser1);
           session.Save(_sampleUser2);
           transaction.Commit();
       }

       SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sampleUser1;
   }

   [Test]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditAddedEntity()
   {
       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Renault&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Clio&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;110000&lt;/span&gt;&lt;span style="color:#000000;"&gt; };
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.AddedDate);
       Assert.IsNotNull(car.AddedBy);
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.AddedFromHostName));
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.AddedFromIP));
   }

   [Test]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditModifiedEntity()
   {
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ford&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ka&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;97544&lt;/span&gt;&lt;span style="color:#000000;"&gt;;

       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName,
               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers };

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.AddedDate);
       Assert.IsNotNull(car.AddedBy);


       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;let&amp;#39;s change logged user and wait 2 seconds to change the car&amp;#39;s data&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;       SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sampleUser2;
       Thread.Sleep(&lt;/span&gt;&lt;span style="color:#800080;"&gt;2000&lt;/span&gt;&lt;span style="color:#000000;"&gt;);

       car.Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;54000&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       car.ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Fiesta&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.ModifiedBy);
       Assert.IsNotNull(car.ModifiedDate);
       Assert.AreNotEqual(car.ModifiedDate, car.AddedDate);
       Assert.AreNotEqual(car.ModifiedBy, car.AddedBy);
       Assert.AreNotEqual(car.Kilometers, kilometers);
       Assert.AreNotEqual(car.ModelName, modelName);
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ModifiedFromHostName));
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ModifiedFromIP));
   }

   [Test]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditErasedEntity()
   {
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Citroen&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;C3&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;74665&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;

       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName,
               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers };

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;
       }

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.Delete(car);
           transaction.Commit();
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.ErasedBy);
       Assert.IsNotNull(car.ErasedDate);
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ErasedFromHostName));
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ErasedFromIP));

       ISession tmpSession &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession();
       tmpSession.EnableFilter(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;avoidLogicalDeleted&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
       Assert.AreEqual(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, tmpSession.CreateCriteria&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;().List().Count);
       tmpSession.DisableFilter(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;avoidLogicalDeleted&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
       Assert.AreEqual(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, tmpSession.CreateCriteria&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;().List().Count);

   }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;This test fixture proves that i am not lying. :-)&lt;/p&gt;
&lt;p&gt;A few last words, in CanAuditErasedEntity() method, at the end of it, it is supposed that if we deleted logically an entity, we don&amp;rsquo;t want to see it in the results, so before doing a Criteria or HQL to get some data, we must enable the filter on the ISession, because filters are disabled by default. I do not know if there is a way to enable it on the ISessionFactory, to avoid manually enable it each time we want to get some data.&lt;/p&gt;
&lt;p&gt;Well, this is the first time i write an article, i hope the next will be better. Sorry for my english.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.2shared.com/file/6380003/67d562c8/AuditExample.html"&gt;&lt;span style="background: SpringGreen;"&gt;Solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Download&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Simple audit metadata and logical deletion of business entities</title><link>http://nhforge.org/wikis/howtonh/simple-audit-metadata-and-logical-deletion-of-business-entities/revision/2.aspx</link><pubDate>Sat, 20 Jun 2009 05:47:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:287</guid><dc:creator>Raul Carlomagno</dc:creator><description>Revision 2 posted to How to by Raul Carlomagno on 20/06/2009 02:47:25 a.m.&lt;br /&gt;
&lt;h2&gt;Simple audit metadata and logical deletion of business entities&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, logical delete, listener, filter&lt;/div&gt;

&lt;p&gt;This article is just about a simple way to achieve auditing and perform logical delete of your sensible data, doing use of the listeners features of NHibernate. Design patterns and arquitecture principles are outside of the scope of the article, this is just an example, take it as a guide.&lt;/p&gt;
&lt;p&gt;Remember the old days when you had to audit data stored on your database, every time you build a query to insert, update or delete data, you had to &amp;ldquo;remember&amp;rdquo; to include the values for auditing (timestamp, user id). It was worse if you had stored procedures (by the way, i hate them) because suppose that was needed to take user credentials from another service to audit, you were limited by Transact-SQL language inside the stored procedure to access that service. Anyway, every time you build a query, you need those metadata values to store them, it could be good if you can centralize the place where you obtain and set those values (remember DRY).&lt;/p&gt;
&lt;p&gt;Maybe, in a strict way, auditing would take place in the business layer, but we are not going to miss this feature that NHibernate has for us, so auditing will take place in the data access layer. With NHibernate we can override its native methods when performing data access, NHibernate will fire events which we will have to override to audit the data and of course to do a logical deletion of our entities.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s get inside the code and explain the domain. In the example we have a BaseEntity for any persistible entity, and then, for the auditable entities, the AuditableEntity which inherits from BaseEntity. We want to audit the cars inside the database. We will audit the date and time, the user, the IP address and the hostname for each action on the entity (insert, update, delete), i think this auditing metada is fine, you can add more of course, your fantasy is your limit.&lt;/p&gt;
&lt;p&gt;In fact this can&amp;rsquo;t be called audit, because we are not tracking changes of entities, just who did an action, when and from, pseudoaudit? [:D]&lt;/p&gt;
&lt;p&gt;&lt;a href="/wikis/howtonh/add.aspx/$image24.png"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img title="image" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="image" src="/wikis/howtonh/add.aspx/$image_thumb16.png" width="407" border="0" height="712" /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="/wikis/howtonh/add.aspx/$image24.png"&gt;&lt;span style="background: SpringGreen;"&gt;&lt;img title="image" style="display:inline;border-width:0px;border:0;" alt="image" src="http://i40.tinypic.com/16iijs.png" width="407" border="0" height="712" /&gt;&lt;/span&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;So, the example solution has a test fixture to test the purpose of the example. It was developed with NHibernate 2.1.0.2001.&lt;/p&gt;
&lt;p&gt;No more words, show me the code.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see the mapping files.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Car.hbm.xml&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:411b4d59-2c28-4172-ab11-be3aabfc1650" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Car&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;cars&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Int64&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;hilo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;max_lo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;50&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;param&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;BrandName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;brand_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModelName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;model_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt;audit attributes&lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;added_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;modified_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; condition&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_date IS NULL&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter-def &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;It has the Id, 3 owned properties, and 9 properties for metadata auditing. The filter part is about logical deletes, we are going to talk about it in later. &lt;/p&gt;
&lt;p&gt;The &lt;b&gt;User.hbm.xml &lt;/b&gt;is really simple, we are not going to show it, it has no sense, anyway, the example solution can be downloaded.&lt;/p&gt;
&lt;p&gt;Now it&amp;rsquo;s time to show the NHibernate configuration file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;hibernate.cfg.xml&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3ba95b40-497a-4b28-8b49-14fc1742bf02" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration  &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      Server=(local)\SQLEXPRESS;initial catalog=auditexample;Integrated Security=SSPI
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;adonet.batch_size&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;10&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;false&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;query.substitutions&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true 1, false 0, yes &amp;#39;Y&amp;#39;, no &amp;#39;N&amp;#39;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomSaveUpdateEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;save-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomDeleteEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;delete&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s analyze it, in the configuration file there is an attribute that is not so common, the listener attribute, as we mention before, with these attributes we are forcing NHibernate to lookup for the custom event listener that we inherited from the default listeners for ISession.Delete() and ISession.SaveUpdate() methods. Inside both class CustomSaveUpdateEventListener and CustomDeleteEventListener will appear a class called SecurityContext which handles the metada for auditing, this class will be discused later, after explaining both listeners.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;CustomSaveUpdateEventListener.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e2d0d747-e26f-4d1c-a83b-59a459662651" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; CustomSaveUpdateEventListener : DefaultSaveOrUpdateEventListener
{
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;override&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; PerformSaveOrUpdate(SaveOrUpdateEvent evt)
   {
       var entity &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; evt.Entity &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditableEntity;

          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (entity &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;){
              &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (entity.Id &lt;/span&gt;&lt;span style="color:#000000;"&gt;==&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
                  ProcessEntityForInserting(entity);
              &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;
                  ProcessEntityForUpdating(entity);
          }
       
          &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;base&lt;/span&gt;&lt;span style="color:#000000;"&gt;.PerformSaveOrUpdate(evt);
   }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;virtual&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForInserting(AuditableEntity entity)
    {
       entity.AddedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;
       entity.AddedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       entity.AddedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();
       entity.AddedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();
    }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;virtual&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForUpdating(AuditableEntity entity)
    {
       entity.ModifiedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;
       entity.ModifiedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       entity.ModifiedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();
       entity.ModifiedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();
    }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;CustomDeleteEventListener.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0d9f2bf9-3a0b-4402-832d-e0e844f30d37" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; CustomDeleteEventListener : DefaultDeleteEventListener
{
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;override&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; DeleteEntity(IEventSource session, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;object&lt;/span&gt;&lt;span style="color:#000000;"&gt; entity,
                        NHibernate.Engine.EntityEntry entityEntry,
                        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color:#000000;"&gt; isCascadeDeleteEnabled,
                        NHibernate.Persister.Entity.IEntityPersister persister,
                        Iesi.Collections.ISet transientEntities)
   {
        var auditableEntity &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; entity &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;as&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditableEntity;

           &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (auditableEntity &lt;/span&gt;&lt;span style="color:#000000;"&gt;!=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;){
              ProcessEntityForLogicalDelete(auditableEntity);
               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.CascadeBeforeDelete(session, persister, entity,
                   entityEntry, transientEntities);
               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;.CascadeAfterDelete(session, persister, entity,
                   transientEntities);
       } &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt; {
            &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;normal delete&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;               &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;base&lt;/span&gt;&lt;span style="color:#000000;"&gt;.DeleteEntity(session, entity, entityEntry,
                isCascadeDeleteEnabled, persister, transientEntities);
       }
   }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ProcessEntityForLogicalDelete(AuditableEntity entity)
   {
        entity.ErasedDate &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; DateTime.Now;
        entity.ErasedBy &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;??&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
        entity.ErasedFromIP &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostIP();
        entity.ErasedFromHostName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext.GetHostName();
   }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;h4&gt;&lt;b&gt;SecurityContext.cs&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:64c46d35-180d-4eea-b740-092564076a3d" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; Ridiculous simple crosscut security class to get user credentials and audit metadata
&lt;/span&gt;&lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color:#808080;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; SecurityContext
{
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;maybe System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(), you decide&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; User LoggedUser {&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;get&lt;/span&gt;&lt;span style="color:#000000;"&gt;; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;set&lt;/span&gt;&lt;span style="color:#000000;"&gt;;}

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetHostName()
    {
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dns.GetHostName();
    }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;static&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; GetHostIP()
   {
        IPAddress[] addresses &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; Dns.GetHostAddresses(Dns.GetHostName());
        
        &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;be carefull and filter the public ip, use the local one
        &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;and of course, this has no sense for an httpcontext, use remote host ip instead&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;if&lt;/span&gt;&lt;span style="color:#000000;"&gt; (addresses.Length &lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;)
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; addresses[&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;].ToString();
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;else&lt;/span&gt;&lt;span style="color:#000000;"&gt;
            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
   }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;This class is just a simple helper to get the metadata needed for auditing, in this example it holds the session of the logged user too.&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a58b502c-190d-4d18-9988-4eed1524b883" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#000000;"&gt;[TestFixture]
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;class&lt;/span&gt;&lt;span style="color:#000000;"&gt; AuditTest
{
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; Configuration _configuration;
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; ISessionFactory _sessionFactory;

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; User _sampleUser1;
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; User _sampleUser2;

   [TestFixtureSetUp]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestFixtureSetUp()
   {
       XmlConfigurator.Configure();

       _configuration &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Configuration();
       _configuration.Configure();
       _sessionFactory &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _configuration.BuildSessionFactory();

       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;drop all tables on database&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;       _sessionFactory.OpenSession().CreateSQLQuery(
               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;IF OBJECT_ID (N&amp;#39;dbo.cars&amp;#39;, N&amp;#39;U&amp;#39;) IS NOT NULL DELETE cars&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        ).ExecuteUpdate();
       
       _sessionFactory.OpenSession().CreateSQLQuery(
               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;IF OBJECT_ID (N&amp;#39;dbo.users&amp;#39;, N&amp;#39;U&amp;#39;) IS NOT NULL DELETE users&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        ).ExecuteUpdate();
       
       _sessionFactory.OpenSession().CreateSQLQuery(
               &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;EXEC sp_MSforeachtable ?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        ).SetParameter(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;,&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;DROP TABLE ?&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).ExecuteUpdate();

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; SchemaExport(_configuration).Create(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;false&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;);

       PrepareLoggedUsers();
   }

   [TestFixtureTearDown]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestFixtureTearDown()
   {
       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;new SchemaExport(_configuration).Drop(false, true);&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;       _sessionFactory.Close();
   }

   [SetUp]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; TestSetUp()
   {
       _sessionFactory.OpenSession().CreateQuery(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;delete Car&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;).ExecuteUpdate();
   }

   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;private&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; PrepareLoggedUsers()
   {
       _sampleUser1 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; User() { Login &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;rcarlomagno&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Raul&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
               LastName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Carlomagno&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Password &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;jijiji&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; };
       _sampleUser2 &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; User() { Login &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;ndaponte&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Name &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Nadia&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
               LastName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Daponte&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, Password &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;jujujuju&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; };

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.Save(_sampleUser1);
           session.Save(_sampleUser2);
           transaction.Commit();
       }

       SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sampleUser1;
   }

   [Test]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditAddedEntity()
   {
       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Renault&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Clio&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;110000&lt;/span&gt;&lt;span style="color:#000000;"&gt; };
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.AddedDate);
       Assert.IsNotNull(car.AddedBy);
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.AddedFromHostName));
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.AddedFromIP));
   }

   [Test]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditModifiedEntity()
   {
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ford&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Ka&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;97544&lt;/span&gt;&lt;span style="color:#000000;"&gt;;

       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName,
               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers };

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.AddedDate);
       Assert.IsNotNull(car.AddedBy);


       &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt;let&amp;#39;s change logged user and wait 2 seconds to change the car&amp;#39;s data&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;       SecurityContext.LoggedUser &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sampleUser2;
       Thread.Sleep(&lt;/span&gt;&lt;span style="color:#800080;"&gt;2000&lt;/span&gt;&lt;span style="color:#000000;"&gt;);

       car.Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;54000&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       car.ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Fiesta&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.ModifiedBy);
       Assert.IsNotNull(car.ModifiedDate);
       Assert.AreNotEqual(car.ModifiedDate, car.AddedDate);
       Assert.AreNotEqual(car.ModifiedBy, car.AddedBy);
       Assert.AreNotEqual(car.Kilometers, kilometers);
       Assert.AreNotEqual(car.ModelName, modelName);
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ModifiedFromHostName));
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ModifiedFromIP));
   }

   [Test]
   &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; CanAuditErasedEntity()
   {
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Citroen&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;C3&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;const&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800080;"&gt;74665&lt;/span&gt;&lt;span style="color:#000000;"&gt;;
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;long&lt;/span&gt;&lt;span style="color:#000000;"&gt; carId;

       Car car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Car() { BrandName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; brandName, ModelName &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; modelName,
               Kilometers &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; kilometers };

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.SaveOrUpdate(car);
           transaction.Commit();
           carId &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; car.Id;
       }

       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ISession session &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession())
       &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;using&lt;/span&gt;&lt;span style="color:#000000;"&gt; (ITransaction transaction &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; session.BeginTransaction())
       {
           session.Delete(car);
           transaction.Commit();
       }

       car &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession().Get&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;(carId);

       Assert.IsNotNull(car);
       Assert.IsNotNull(car.ErasedBy);
       Assert.IsNotNull(car.ErasedDate);
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ErasedFromHostName));
       Assert.IsFalse(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;.IsNullOrEmpty(car.ErasedFromIP));

       ISession tmpSession &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; _sessionFactory.OpenSession();
       tmpSession.EnableFilter(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;avoidLogicalDeleted&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
       Assert.AreEqual(&lt;/span&gt;&lt;span style="color:#800080;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;, tmpSession.CreateCriteria&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;().List().Count);
       tmpSession.DisableFilter(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;avoidLogicalDeleted&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
       Assert.AreEqual(&lt;/span&gt;&lt;span style="color:#800080;"&gt;1&lt;/span&gt;&lt;span style="color:#000000;"&gt;, tmpSession.CreateCriteria&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;Car&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;().List().Count);

   }
}&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;This test fixture proves that i am not lying. :-)&lt;/p&gt;
&lt;p&gt;A few last words, in CanAuditErasedEntity() method, at the end of it, it is supposed that if we deleted logically an entity, we don&amp;rsquo;t want to see it in the results, so before doing a Criteria or HQL to get some data, we must enable the filter on the ISession, because filters are disabled by default. I do not know if there is a way to enable it on the ISessionFactory, to avoid manually enable it each time we want to get some data.&lt;/p&gt;
&lt;p&gt;Well, this is the first time i write an article, i hope the next will be better. Sorry for my english.&lt;/p&gt;</description></item><item><title>Simple audit metadata and logical deletion of business entities</title><link>http://nhforge.org/wikis/howtonh/simple-audit-metadata-and-logical-deletion-of-business-entities/revision/1.aspx</link><pubDate>Sat, 20 Jun 2009 05:44:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:286</guid><dc:creator>Raul Carlomagno</dc:creator><description>Revision 1 posted to How to by Raul Carlomagno on 20/06/2009 02:44:25 a.m.&lt;br /&gt;
&lt;p&gt;This article is just about a simple way to achieve auditing and perform logical delete of your sensible data, doing use of the listeners features of NHibernate. Design patterns and arquitecture principles are outside of the scope of the article, this is just an example, take it as a guide.&lt;/p&gt;
&lt;p&gt;Remember the old days when you had to audit data stored on your database, every time you build a query to insert, update or delete data, you had to &amp;ldquo;remember&amp;rdquo; to include the values for auditing (timestamp, user id). It was worse if you had stored procedures (by the way, i hate them) because suppose that was needed to take user credentials from another service to audit, you were limited by Transact-SQL language inside the stored procedure to access that service. Anyway, every time you build a query, you need those metadata values to store them, it could be good if you can centralize the place where you obtain and set those values (remember DRY).&lt;/p&gt;
&lt;p&gt;Maybe, in a strict way, auditing would take place in the business layer, but we are not going to miss this feature that NHibernate has for us, so auditing will take place in the data access layer. With NHibernate we can override its native methods when performing data access, NHibernate will fire events which we will have to override to audit the data and of course to do a logical deletion of our entities.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s get inside the code and explain the domain. In the example we have a BaseEntity for any persistible entity, and then, for the auditable entities, the AuditableEntity which inherits from BaseEntity. We want to audit the cars inside the database. We will audit the date and time, the user, the IP address and the hostname for each action on the entity (insert, update, delete), i think this auditing metada is fine, you can add more of course, your fantasy is your limit.&lt;/p&gt;
&lt;p&gt;In fact this can&amp;rsquo;t be called audit, because we are not tracking changes of entities, just who did an action, when and from, pseudoaudit? [:D]&lt;/p&gt;
&lt;p&gt;&lt;a href="/wikis/howtonh/add.aspx/$image24.png"&gt;&lt;img title="image" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" alt="image" src="/wikis/howtonh/add.aspx/$image_thumb16.png" width="407" border="0" height="712" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;So, the example solution has a test fixture to test the purpose of the example. It was developed with NHibernate 2.1.0.2001.&lt;/p&gt;
&lt;p&gt;No more words, show me the code.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see the mapping files.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;Car.hbm.xml&lt;/b&gt;&lt;/h4&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:411b4d59-2c28-4172-ab11-be3aabfc1650" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-mapping-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
                   namespace&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;class &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Car&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; table&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;cars&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;id &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Int64&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;hilo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;max_lo&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;50&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;param&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;generator&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;id&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;BrandName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;brand_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModelName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;model_name&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;Kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;kilometers&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt;audit attributes&lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;added_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AddedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;added_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;modified_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ModifiedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;modified_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
    
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;many-to-one &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedBy&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.Entities.User, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        column&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_user_id&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; not-null&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedDate&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_date&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromIP&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_ip&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;ErasedFromHostName&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; column &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;= &amp;quot;erased_from_hostname&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; condition&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;erased_date IS NULL&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;filter-def &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;avoidLogicalDeleted&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;It has the Id, 3 owned properties, and 9 properties for metadata auditing. The filter part is about logical deletes, we are going to talk about it in later. &lt;/p&gt;
&lt;p&gt;The &lt;b&gt;User.hbm.xml &lt;/b&gt;is really simple, we are not going to show it, it has no sense, anyway, the example solution can be downloaded.&lt;/p&gt;
&lt;p&gt;Now it&amp;rsquo;s time to show the NHibernate configuration file.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;hibernate.cfg.xml&lt;/b&gt;&lt;/h4&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3ba95b40-497a-4b28-8b49-14fc1742bf02" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:auto;"&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration  &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      Server=(local)\SQLEXPRESS;initial catalog=auditexample;Integrated Security=SSPI
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;adonet.batch_size&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;10&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;false&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;query.substitutions&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true 1, false 0, yes &amp;#39;Y&amp;#39;, no &amp;#39;N&amp;#39;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomSaveUpdateEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;save-update&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;listener &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;class&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;AuditExample.NHExtensions.CustomDeleteEventListener, AuditExample&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
        type&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&amp;quot;delete&amp;quot;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&amp;lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&amp;gt;&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s analyze it, in the configuration file there is an attribute that is not so common, the listener attribute, as we mention before, with these attributes we are forcing NHibernate to lookup for the custom event listener that we inherited from the default listeners for ISession.Delete() and ISession.SaveUpdate() methods. Inside both class CustomSaveUpdateEventListener and CustomDeleteEventListener will appear a class called SecurityContext which handles the metada for auditing, this class will be discused later, after explaining both listeners.&lt;/p&gt;
&lt;h4&gt;&lt;b&gt;CustomSaveUpdate
