<rss version="2.0">
  <channel>
    <title>Fluent NHibernate</title>
    <link>http://wiki.fluentnhibernate.org/show/HomePage</link>
    <description>An Instiki wiki</description>
    <language>en-us</language>
    <ttl>40</ttl>
    <item>
      <title>Auto Mapping Conventions</title>
      <description>&lt;p&gt;Auto mappings are generated based on a set of conventions, assumptions about your environment, that mean you can map your entire domain with a miniscule amount of code. Sometimes however, the conventions we supply are not to your liking, perhaps you're a control freak and want 100% control, or more likely you're working against an existing database that has it's own set of standards. You'd still like to use the auto mapper, but can't because it maps your entities all wrong.&lt;/p&gt;

&lt;p&gt;Luckily for you we've thought about that, you can customise the conventions that the auto mapper uses. To see more about conventions in general, read the &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;We'll continue with our store example from before (in &lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Auto Mapping&lt;/a&gt;), which comprised of a &lt;code&gt;Product&lt;/code&gt; and a &lt;code&gt;Shelf&lt;/code&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class Product  
{  
  public int Id { get; private set; }  
  public virtual string Name { get; set; }  
  public virtual decimal Price { get; set; }  
}  

public class Shelf  
{  
  public virtual int Id { get; private set; }  
  public virtual IList&amp;lt;Product&amp;gt; Products { get; private set; }  

  public Shelf()
  {
    Products = new List&amp;lt;Product&amp;gt;();
  }
}&lt;/pre&gt;

&lt;p&gt;Using the standard auto mapping conventions, this assumes a database schema like so:&lt;/p&gt;

&lt;pre&gt;
table Product (
  Id int identity primary key,
  Name varchar(100),
  Price decimal,
  Shelf_id int foreign key
)

table Shelf (
  Id int identity primary key
)&lt;/pre&gt;

&lt;p&gt;Nothing too complicated there. The auto mapper has correctly assumed that our Ids are identity's and are primary keys, it's also assumed their names, the name of our foreign key to the &lt;code&gt;Shelf&lt;/code&gt; table (&lt;code&gt;ShelfId&lt;/code&gt;), and the length of our &lt;code&gt;Name&lt;/code&gt; column.&lt;/p&gt;

&lt;p&gt;Lets assume for the sake of this post that you're not happy with that schema. You're one of those people that prefers to name their primary key after the table it's in, so our &lt;code&gt;Product&lt;/code&gt; identity should be called &lt;code&gt;ProductId&lt;/code&gt;; also, you like your foreign key's to be explicitly named _FK, and your strings are always a bit longer than &lt;code&gt;100&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember this fellow?&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;Product&amp;gt;()  
  .Where(t =&amp;gt; t.Namespace == "Storefront.Entities");&lt;/pre&gt;

&lt;p&gt;Lets update it to include some convention overrides. We'll start with the Id name. The conventions we're about to implement are better explained in the &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; wiki.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;Product&amp;gt;()
  .Where(t =&amp;gt; t.Namespace == "Storefront.Entities")
  .ConventionDiscovery.Add&amp;lt;PrimaryKeyConvention&gt;();&lt;/pre&gt;

&lt;p&gt;We've added a convention to the convention discovery mechanism, now let's implement it.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class PrimaryKeyConvention
  : IIdConvention
{
  public bool Accept(IIdentityPart id)
  {
    return true;
  }

  public void Apply(IIdentityPart id)
  {
    id.ColumnName(id.Property.ReflectedType.Name + "Id");
  }
}&lt;/pre&gt;

&lt;p&gt;Our &lt;code&gt;PrimaryKeyConvention&lt;/code&gt; gets applied to all Ids, and sets their column name based on the &lt;code&gt;ReflectedType&lt;/code&gt; of the Id property. This is a fancy way of saying get the name of the class the Id property is in. Our primary key's will now be generated as &lt;code&gt;TypeNameId&lt;/code&gt;; which means our schema now looks like this:&lt;/p&gt;

&lt;pre&gt;
table Product (
  ProductId int identity primary key,
  Name varchar(100),
  Price decimal,
  Shelf_id int foreign key
)

table Shelf (
  ShelfId int identity primary key
)&lt;/pre&gt;

&lt;p&gt;As you can see, our primary key's now have our desired naming convention. Lets do the other two together, as they're so simple; we'll override the foreign key naming, and change the default length for strings.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.ConventionDiscovery.Setup(c =&amp;gt;
  c.Add&amp;lt;PrimaryKeyConvention&amp;gt;();
  c.Add&amp;lt;ForeignKeyConvention&amp;gt();
  c.Add&amp;lt;DefaultStringLengthConvention&amp;gt;();
});

public class ForeignKeyConvention
  : IHasManyConvention
{
  public bool Apply(IOneToManyPart part)
  {
    return true;
  }

  public void Accept(IOneToManyPart part)
  {
    part.WithKeyColumn(part.ParentType.Name + "_FK");
  }
}

public class DefaultStringLengthConvention
  : IPropertyConvention
{
  public bool Apply(IProperty property)
  {
    return true;
  }

  public void Accept(IProperty property)
  {
    property.WithLengthOf(250);
  }
}&lt;/pre&gt;

&lt;p&gt;That's all there is to it, when combined with the other conventions you can customise the mappings quite heavily while only adding a few lines to your auto mapping.&lt;/p&gt;

&lt;p&gt;This is our final schema:&lt;/p&gt;

&lt;pre&gt;
table Product (
  ProductId int identity primary key,
  Name varchar(250),
  Price decimal,
  Shelf_FK int foreign key
)

table Shelf (
  ShelfId int identity primary key
)&lt;/pre&gt;

&lt;p&gt;To see more about conventions, read the &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; and &lt;a class="existingWikiWord" href="/show/ConvertingToNewStyleConventions"&gt;Converting To New Style Conventions&lt;/a&gt; pages.&lt;/p&gt;

&lt;p&gt;What next? &lt;a class="existingWikiWord" href="/show/AutoMappingTypeConventions"&gt;Auto Mapping Type Conventions&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 10 Apr 2009 04:19:42 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/AutoMappingConventions</guid>
      <link>http://wiki.fluentnhibernate.org/show/AutoMappingConventions</link>
    </item>
    <item>
      <title>Standard Mapping</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Fluent Mapping&lt;/strong&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/StandardMapping"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingClassMap"&gt;ClassMap&amp;lt;T&amp;gt;&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingIds"&gt;Ids&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingProperties"&gt;Properties&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingRelationships"&gt;Relationships&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingComponents"&gt;Components&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingConventions"&gt;Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingPrivateProperties"&gt;Private Properties&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;p&gt;Fluent NHibernate has two mapping styles: &lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Auto Mapping&lt;/a&gt; and Standard Mapping (aka non-auto mapping, or just fluent mapping). This page is about the latter.&lt;/p&gt;

&lt;p&gt;The &lt;a class="existingWikiWord" href="/show/GettingStarted%3A+First+Project"&gt;GettingStarted: First Project&lt;/a&gt; example uses fluent mapping for its mappings, so you should refer to that if you've not done any work mappings before; if you're looking for some more complex examples then this is the place.&lt;/p&gt;

&lt;p&gt;You can read about the &lt;a href="StandardMappingClassMap"&gt;ClassMap&amp;lt;T&gt;&lt;/a&gt;, mapping &lt;a href="StandardMappingProperties"&gt;properties&lt;/a&gt;, mapping &lt;a href="StandardMappingRelationships"&gt;relationships&lt;/a&gt; and more from the right hand menu.&lt;/p&gt;

&lt;p&gt;For a growing collection of common mapping situations, have a look at &lt;a class="existingWikiWord" href="/show/MappingPatterns"&gt;Mapping Patterns&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Thu, 09 Apr 2009 23:12:04 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/StandardMapping</guid>
      <link>http://wiki.fluentnhibernate.org/show/StandardMapping</link>
    </item>
    <item>
      <title>Getting Started: First Project</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Introduction"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Install"&gt;Install&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/GettingStarted%3A+First+Project"&gt;First Project&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;blockquote&gt;
    &lt;p&gt;All the source can be found in the main Fluent NHibernate solution, in the &lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject"&gt;Example.FirstProject&lt;/a&gt; project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You need to have Fluent NHibernate already downloaded and compiled to follow this guide, if you haven't done that yet then please refer to &lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Install"&gt;GettingStarted: Install&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For this example we're going to be mapping a simple domain for a retail company. The company has a couple of stores, each with products in (some products are in both stores, some are exclusive), and each with employees. In database terms, that's a &lt;code&gt;Store&lt;/code&gt;, &lt;code&gt;Employee&lt;/code&gt;, and &lt;code&gt;Product&lt;/code&gt; table, with a many-to-many table between &lt;code&gt;Store&lt;/code&gt; and &lt;code&gt;Product&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img alt="FirstProjectSchema.png" src="/files/FirstProjectSchema.png" /&gt;&lt;/p&gt;

&lt;p&gt;First, create a console application and reference the &lt;code&gt;FluentNHibernate.dll&lt;/code&gt; you built earlier, and whichever version of &lt;code&gt;NHibernate.dll&lt;/code&gt; you built against (if you're unsure, just use the one that's output with &lt;code&gt;FluentNHibernate.dll&lt;/code&gt;); also, because for this example we're going to be using a &lt;a href="http://www.sqlite.org/"&gt;SQLite&lt;/a&gt; database, you'll need the &lt;a href="http://sourceforge.net/projects/sqlite-dotnet2"&gt;System.Data.SQLite&lt;/a&gt; library which is distributed with Fluent NHibernate.&lt;/p&gt;

&lt;div style="float: left;"&gt;
&lt;img alt="FirstProjectLayout.png" src="/files/FirstProjectLayout.png" /&gt;
&lt;/div&gt;

&lt;p&gt;You can see the project structure that I used to the left. The &lt;code&gt;Entities&lt;/code&gt; folder is for your actual domain objects, while the &lt;code&gt;Mappings&lt;/code&gt; folder is where we're going to put your fluent mapping classes.&lt;/p&gt;

&lt;p&gt;For the rest of this guide I'm going to assume you've used the same structure.&lt;/p&gt;

&lt;h3 id="the_entities" style="clear: left;"&gt;The entities&lt;/h3&gt;

&lt;p&gt;Now that we've got our project setup, lets start by creating our entities. We've got three tables we need to map (we're ignoring the many-to-many join table right now), so that's one entity per table. Create the following classes in your &lt;code&gt;Entities&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Entities/Employee.cs"&gt;Entities/Employee.cs&lt;/a&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class Employee
{
  public virtual int Id { get; private set; }
  public virtual string FirstName { get; set; }
  public virtual string LastName { get; set; }
  public virtual Store Store { get; set; }
}
&lt;/pre&gt;

&lt;p&gt;Our &lt;code&gt;Employee&lt;/code&gt; entity has an &lt;code&gt;Id&lt;/code&gt;, the person's name (split over &lt;code&gt;FirstName&lt;/code&gt; and &lt;code&gt;LastName&lt;/code&gt;), and finally a reference to the &lt;code&gt;Store&lt;/code&gt; that they work in.&lt;/p&gt;

&lt;p&gt;There's two things that may stand out to you if you're unfamiliar with NHibernate. Firstly, the &lt;code&gt;Id&lt;/code&gt; property has a private setter, this is because it's only NHibernate that should be setting the value of that &lt;code&gt;Id&lt;/code&gt;. Secondly, all the properties are marked &lt;code&gt;virtual&lt;/code&gt;; this is because NHibernate creates "proxies" of your entities at run time to allow for lazy loading, and for it to do that it needs to be able to override the properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Entities/Product.cs"&gt;Entities/Product.cs&lt;/a&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class Product
{
  public virtual int Id { get; private set; }
  public virtual string Name { get; set; }
  public virtual double Price { get; set; }
  public virtual IList&amp;lt;Store&amp;gt; StoresStockedIn { get; private set; }

  public Product()
  {
    StoresStockedIn = new List&amp;lt;Store&amp;gt;();
  }
}&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;Product&lt;/code&gt; has an &lt;code&gt;Id&lt;/code&gt;, it's &lt;code&gt;Name&lt;/code&gt;, &lt;code&gt;Price&lt;/code&gt;, and a collection of &lt;code&gt;Store&lt;/code&gt;s that stock it.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Entities/Store.cs"&gt;Entities/Store.cs&lt;/a&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class Store
{
  public virtual int Id { get; private set; }
  public virtual string Name { get; set; }
  public virtual IList&amp;lt;Product&amp;gt; Products { get; set; }
  public virtual IList&amp;lt;Employee&amp;gt; Staff { get; set; }

  public Store()
  {
    Products = new List&amp;lt;Product&amp;gt;();
    Staff = new List&amp;lt;Employee&amp;gt;();
  }

  public virtual void AddProduct(Product product)
  {
    product.StoresStockedIn.Add(this);
    Products.Add(product);
  }

  public virtual void AddEmployee(Employee employee)
  {
    employee.Store = this;
    Staff.Add(employee);
  }
}&lt;/pre&gt;

&lt;p&gt;Finally, we've got our &lt;code&gt;Store&lt;/code&gt; entity. This has an &lt;code&gt;Id&lt;/code&gt; and a &lt;code&gt;Name&lt;/code&gt;, along with a collection of &lt;code&gt;Product&lt;/code&gt;s that are stocked in it, as well as a collection of &lt;code&gt;Employee&lt;/code&gt;s (in the &lt;code&gt;Staff&lt;/code&gt; list)  that work there. This entity has a little bit of logic in it to make our code simpler, that's the &lt;code&gt;AddProduct&lt;/code&gt; and &lt;code&gt;AddEmployee&lt;/code&gt; methods; these methods are used to add items into the collections, and setup the other side of the relationships.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;If you're an NHibernate veteran then you'll recognise this; however, if it's all new to you then let me explain: in a relationship where both sides are mapped, NHibernate needs you to set both sides before it will save correctly. So as to not have this extra code everywhere, it's been reduced to these extra methods in &lt;code&gt;Store&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;The mappings&lt;/h3&gt;

&lt;p&gt;Now that we've got our entities created, it's time to map them using Fluent NHibernate. We'll start with the simplest class, which is &lt;code&gt;Employee&lt;/code&gt;. All the following mappings should be created inside the &lt;code&gt;Mappings&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;To map an entity, you have to create a dedicated mapping class (typically this follows the naming convention of EntityNameMap), so we'll create an &lt;code&gt;EmployeeMap&lt;/code&gt; class; these mapping classes have to derive from &lt;code&gt;ClassMap&amp;lt;T&amp;gt;&lt;/code&gt; where &lt;code&gt;T&lt;/code&gt; is your entity.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Mappings/EmployeeMap.cs"&gt;Mappings/EmployeeMap.cs&lt;/a&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class EmployeeMap : ClassMap&amp;lt;Employee&amp;gt;
{

}&lt;/pre&gt;

&lt;p&gt;The mappings themselves are done inside the constructor for &lt;code&gt;EmployeeMap&lt;/code&gt;, so we need to add a constructor and write the mappings.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class EmployeeMap : ClassMap&amp;lt;Employee&amp;gt;
{
  public EmployeeMap()
  {
    Id(x =&gt; x.Id);
  }
}&lt;/pre&gt;

&lt;p&gt;To start with we've mapped the &lt;code&gt;Id&lt;/code&gt; column, and told Fluent NHibernate that it's actually an identifier. The &lt;code&gt;x&lt;/code&gt; in this example is an instance of &lt;code&gt;Employee&lt;/code&gt; that Fluent NHibernate uses to retrieve the property details from, so all you're really doing here is telling it which property you want your Id to be. Fluent NHibernate will see that your &lt;code&gt;Id&lt;/code&gt; property has a type of &lt;code&gt;int&lt;/code&gt;, and it'll automatically decide that it should be mapped as an auto-incrementing identity in the database - handy!&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;For NHibernate users, that means it automatically creates the &lt;code&gt;generator&lt;/code&gt; element as &lt;code&gt;identity&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets map the rest of &lt;code&gt;Employee&lt;/code&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class EmployeeMap : ClassMap&amp;lt;Employee&amp;gt;
{
  public EmployeeMap()
  {
    Id(x =&gt; x.Id);
    Map(x =&gt; x.FirstName);
    Map(x =&gt; x.LastName);
    References(x =&gt; x.Store);
  }
}&lt;/pre&gt;

&lt;p&gt;There are a couple of new methods we've used there, &lt;code&gt;Map&lt;/code&gt; and &lt;code&gt;References&lt;/code&gt;; &lt;code&gt;Map&lt;/code&gt; creates a mapping for a simple property, while &lt;code&gt;References&lt;/code&gt; creates a many-to-one relationship between two entities. In this case we've mapped &lt;code&gt;FirstName&lt;/code&gt; and &lt;code&gt;LastName&lt;/code&gt; as simple properties, and created a many-to-one to &lt;code&gt;Store&lt;/code&gt; (many &lt;code&gt;Employee&lt;/code&gt;s to one &lt;code&gt;Store&lt;/code&gt;) through the &lt;code&gt;Store&lt;/code&gt; property.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;NHibernate users: &lt;code&gt;Map&lt;/code&gt; is equivalent to the &lt;code&gt;property&lt;/code&gt; element and &lt;code&gt;References&lt;/code&gt; to &lt;code&gt;many-to-one&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets carry on by mapping the &lt;code&gt;Store&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Mappings/StoreMap.cs"&gt;Mappings/StoreMap.cs&lt;/a&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class StoreMap : ClassMap&amp;lt;Store&amp;gt;
{
  public StoreMap()
  {
    Id(x =&gt; x.Id);
    Map(x =&gt; x.Name);
    HasMany(x =&gt; x.Staff)
      .Inverse()
      .Cascade.All();
    HasManyToMany(x =&gt; x.Products)
     .Cascade.All()
     .WithTableName("StoreProduct");
  }
}&lt;/pre&gt;

&lt;p&gt;Again, there's a couple of new calls here. If you remember back to &lt;code&gt;Employee&lt;/code&gt;, we created a many-to-one relationship with &lt;code&gt;Store&lt;/code&gt;, well now that we're mapping &lt;code&gt;Store&lt;/code&gt; we can create the other side of that relationship. So &lt;code&gt;HasMany&lt;/code&gt; is creating a one-to-many relationship with &lt;code&gt;Employee&lt;/code&gt; (one &lt;code&gt;Store&lt;/code&gt; to many &lt;code&gt;Employee&lt;/code&gt;s), which is the other side of the &lt;code&gt;Employee.Store&lt;/code&gt; relationship. The other new method is &lt;code&gt;HasManyToMany&lt;/code&gt;, which creates a many-to-many relationship with &lt;code&gt;Product&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You've also just got your first taste of the fluent interface Fluent NHibernate provides. The &lt;code&gt;HasMany&lt;/code&gt; method has a second call directly from it's return type (&lt;code&gt;Inverse()&lt;/code&gt;), and &lt;code&gt;HasManyToMany&lt;/code&gt; has &lt;code&gt;Cascade.All()&lt;/code&gt; and &lt;code&gt;WithTableName&lt;/code&gt;; this is called method chaining, and it's used to create a more natural language in your configuration.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Inverse&lt;/code&gt; on &lt;code&gt;HasMany&lt;/code&gt; is an NHibernate term, and it means that the other end of the relationship is responsible for saving.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Cascade.All&lt;/code&gt; on &lt;code&gt;HasManyToMany&lt;/code&gt; tells NHibernate to cascade events down to the entities in the collection (so when you save the &lt;code&gt;Store&lt;/code&gt;, all the &lt;code&gt;Product&lt;/code&gt;s are saved too).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;WithTableName&lt;/code&gt; sets the many-to-many join table name.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
    &lt;p&gt;The &lt;code&gt;WithTableName&lt;/code&gt; call is currently only required if you're doing a bidirectional many-to-many, because Fluent NHibernate currently can't guess what the name should be; for all other associations it isn't required.&lt;/p&gt;
    
    &lt;p&gt;For NHibernaters: &lt;code&gt;HasMany&lt;/code&gt; maps to a &lt;code&gt;bag&lt;/code&gt; by default, and has a &lt;code&gt;one-to-many&lt;/code&gt; element inside; &lt;code&gt;HasManyToMany&lt;/code&gt; is the same, but with a &lt;code&gt;many-to-many&lt;/code&gt; element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally, lets map the &lt;code&gt;Product&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Mappings/ProductMap.cs"&gt;Mappings/ProductMap.cs&lt;/a&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class ProductMap : ClassMap&amp;lt;Product&amp;gt;
{
  public ProductMap()
  {
    Id(x =&gt; x.Id);
    Map(x =&gt; x.Name);
    Map(x =&gt; x.Price);
    HasManyToMany(x =&gt; x.StoresStockedIn)
      .Cascade.All()
      .Inverse()
      .WithTableName("StoreProduct");
  }
}&lt;/pre&gt;

&lt;p&gt;That's the &lt;code&gt;Product&lt;/code&gt; mapped; in this case we've used only methods that we've already encountered. The &lt;code&gt;HasManyToMany&lt;/code&gt; is setting up the other side of the bidirectional many-to-many relationship with &lt;code&gt;Store&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;The application&lt;/h3&gt;

&lt;p&gt;In this section we'll initialise some data and output it to the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Program.cs"&gt;Program.cs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
&lt;pre name="code" class="c-sharp"&gt;
static void Main()
{
  var sessionFactory = CreateSessionFactory();

  using (var session = sessionFactory.OpenSession())
  {
    using (var transaction = session.BeginTransaction())
    {
      // create a couple of Stores each with some Products and Employees
      var barginBasin = new Store { Name = "Bargin Basin" };
      var superMart = new Store { Name = "SuperMart" };

      var potatoes = new Product { Name = "Potatoes", Price = 3.60 };
      var fish = new Product { Name = "Fish", Price = 4.49 };
      var milk = new Product { Name = "Milk", Price = 0.79 };
      var bread = new Product { Name = "Bread", Price = 1.29 };
      var cheese = new Product { Name = "Cheese", Price = 2.10 };
      var waffles = new Product { Name = "Waffles", Price = 2.41 };

      var daisy = new Employee { FirstName = "Daisy", LastName = "Harrison" };
      var jack = new Employee { FirstName = "Jack", LastName = "Torrance" };
      var sue = new Employee { FirstName = "Sue", LastName = "Walkters" };
      var bill = new Employee { FirstName = "Bill", LastName = "Taft" };
      var joan = new Employee { FirstName = "Joan", LastName = "Pope" };

      // add products to the stores, there's some crossover in the products in each
      // store, because the store-product relationship is many-to-many
      AddProductsToStore(barginBasin, potatoes, fish, milk, bread, cheese);
      AddProductsToStore(superMart, bread, cheese, waffles);

      // add employees to the stores, this relationship is a one-to-many, so one
      // employee can only work at one store at a time
      AddEmployeesToStore(barginBasin, daisy, jack, sue);
      AddEmployeesToStore(superMart, bill, joan);

      // save both stores, this saves everything else via cascading
      session.SaveOrUpdate(barginBasin);
      session.SaveOrUpdate(superMart);

      transaction.Commit();
    }

    // retreive all stores and display them
    using (session.BeginTransaction())
    {
      var stores = session.CreateCriteria(typeof(Store))
        .List&amp;lt;Store&amp;gt;();

      foreach (var store in stores)
      {
        WriteStorePretty(store);
      }
    }

    Console.ReadKey();
  }
}

public static void AddProductToStore(Store store, params Product[] products)
{
  foreach (var product in products)
  {
    store.AddProduct(product);
  }
}

public static void AddEmployeeToStore(Store store, params Employee[] employees)
{
  foreach (var employee in employees)
  {
    store.AddEmployee(employee);
  }
}&lt;/pre&gt;
&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;For brevity i've left out the definition of &lt;code&gt;WritePrettyStore&lt;/code&gt; which simply calls &lt;code&gt;Console.Write&lt;/code&gt; for the various relationships on a &lt;code&gt;Store&lt;/code&gt; (but you can see it in the &lt;a href="http://code.google.com/p/fluent-nhibernate/source/browse/trunk/src/Examples.FirstProject/Program.cs"&gt;full code&lt;/a&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the &lt;code&gt;Main&lt;/code&gt; method from your &lt;code&gt;Program.cs&lt;/code&gt;. It's a bit lengthy, but what we're doing is creating a couple of &lt;code&gt;Store&lt;/code&gt; instances, then adds some &lt;code&gt;Employee&lt;/code&gt;s and &lt;code&gt;Product&lt;/code&gt;s to them, then saves; finally, it re-queries them from the database and writes them out to the console.&lt;/p&gt;

&lt;p&gt;You won't be able to run this yet, because there's one thing left to do. We need to implement the &lt;code&gt;CreateSessionFactory&lt;/code&gt; method; that's where our configuration goes to tie NHibernate and Fluent NHibernate together.&lt;/p&gt;

&lt;h3&gt;The configuration&lt;/h3&gt;

&lt;p&gt;Lets implement the &lt;code&gt;CreateSessionFactory&lt;/code&gt; method.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
private static ISessionFactory CreateSessionFactory()
{

}&lt;/pre&gt;

&lt;p&gt;That's the method signature sorted, you'll note it's returning an NHibernate &lt;code&gt;ISessionFactory&lt;/code&gt;. Now we're going to use the Fluent NHibernate &lt;code&gt;Fluently.Configure&lt;/code&gt; API to configure our application. You can see more examples on this in the &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration&lt;/a&gt; wiki page. &lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .BuildSessionFactory();
}&lt;/pre&gt;

&lt;p&gt;That's not quite right yet, we're creating a &lt;code&gt;SessionFactory&lt;/code&gt;, but we haven't configured anyting yet; so lets configure our database.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .Database(
      SQLiteConfiguration.Standard
        .UsingFile("firstProject.db")
    )
    .BuildSessionFactory();
}&lt;/pre&gt;

&lt;p&gt;There we go, we've specified that we're using a file-based SQLite database. You can learn more about the database configuration API in the &lt;a class="existingWikiWord" href="/show/DatabaseConfiguration"&gt;Database Configuration&lt;/a&gt; wiki page.&lt;/p&gt;

&lt;p&gt;Just one more thing to go, we need to supply NHibernate with the mappings we've created. To do that, we add a call to &lt;code&gt;Mappings&lt;/code&gt; in our configuration.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .Database(
      SQLiteConfiguration.Standard
        .UsingFile("firstProject.db")
    )
    .Mappings(m =&amp;gt;
      m.FluentMappings.AddFromAssemblyOf&amp;lt;Program&amp;gt;())
    .BuildSessionFactory();
}&lt;/pre&gt;

&lt;p&gt;That's it, that's your application configured!&lt;/p&gt;

&lt;p&gt;You should now be able to run the application and see the results of your query output to the console window.&lt;/p&gt;

&lt;pre class="console"&gt;
Bargin Basin
  Products:
    Potatoes
    Fish
    Milk
    Bread
    Cheese
  Staff:
    Daisy Harrison
    Jack Torrance
    Sue Walkters

SuperMart
  Products:
    Bread
    Cheese
    Waffles
  Staff:
    Bill Taft
    Joan Pope&lt;/pre&gt;

&lt;p&gt;There you go, that's your first Fluent NHibernate project created and running!&lt;/p&gt;

&lt;h3&gt;The schema&lt;/h3&gt;

&lt;p&gt;If you haven't manually created the schema for this application, then it will fail on the first time you run it. There's something you can do about that, but it needs to be done directly against the NHibernate &lt;code&gt;Configuration&lt;/code&gt; object; we can do that using the &lt;code&gt;ExposeConfiguration&lt;/code&gt; method. Combine that call with a method to generate the schema, then you're able to create your schema at runtime.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .Database(
      SQLiteConfiguration.Standard
        .UsingFile("firstProject.db")
    )
    .Mappings(m =&amp;gt;
      m.FluentMappings.AddFromAssemblyOf&amp;lt;Program&amp;gt;())
    .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();
}

private static void BuildSchema(Configuration config)
{
  // delete the existing db on each run
  if (File.Exists(DbFile))
    File.Delete(DbFile);

  // this NHibernate tool takes a configuration (with mapping info in)
  // and exports a database schema from it
  new SchemaExport(config)
    .Create(false, true);
}&lt;/pre&gt;

&lt;p&gt;You can read more about this in the &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration&lt;/a&gt; wiki page.&lt;/p&gt;

&lt;p&gt;What next? &lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Existing+Application"&gt;GettingStarted: Existing Application&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 30 Mar 2009 22:38:10 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/GettingStarted%3A+First+Project</guid>
      <link>http://wiki.fluentnhibernate.org/show/GettingStarted%3A+First+Project</link>
    </item>
    <item>
      <title>Rake Script</title>
      <description>&lt;p&gt;Fluent NHibernate is distributed with a &lt;a href="http://ruby-lang.org/"&gt;Ruby&lt;/a&gt; &lt;a href="http://rake.rubyforge.org/"&gt;Rake&lt;/a&gt; script, which can be used to build without having to open Visual Studio; more importantly, it also provides a simple mechanism for switching versions of NHibernate (see &lt;a class="existingWikiWord" href="/show/SupportedNHiberateVersions"&gt;Supported NHiberate Versions&lt;/a&gt; for more details on our versioning policy).&lt;/p&gt;

&lt;h2&gt;Building with rake&lt;/h2&gt;

&lt;p&gt;Before you can run the rake script, you need to make sure you have all the necessary &lt;a href="http://www.rubygems.org/"&gt;gems&lt;/a&gt; installed; the easiest way to do that is to run the &lt;code&gt;InstallGems.bat&lt;/code&gt; distributed with Fluent NHibernate.&lt;/p&gt;

&lt;p&gt;Once you've got all the gems installed, you can either run &lt;code&gt;Build.bat&lt;/code&gt; which just runs rake, or you can run rake directly: &lt;code class="console"&gt;rake&lt;/code&gt;. Either of those will use &lt;a href="http://en.wikipedia.org/wiki/MSBuild"&gt;MSBuild&lt;/a&gt; to build the solution, and then will run the unit tests before copying the built files to the &lt;code&gt;build&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;When the build is complete, you can reference the Fluent NHibernate binaries from the &lt;code&gt;build&lt;/code&gt; directory.&lt;/p&gt;

&lt;h2&gt;Switching versions&lt;/h2&gt;

&lt;p&gt;The rake script has the capability to switch which version of NHibernate it builds against. Currently we have support for 2.0.1GA and a slightly out of date version of trunk (useful, I know).&lt;/p&gt;

&lt;p&gt;To build against a different version, you need to specify which version when calling rake. &lt;/p&gt;

&lt;p&gt;For 2.0.1GA you can either just call &lt;code class="console"&gt;rake&lt;/code&gt;, because it's the default, or to build it explicitly you can use:&lt;/p&gt;

&lt;pre class="console"&gt;rake default&lt;/pre&gt;

&lt;p&gt;For the 2.1 trunk of NHibernate, you can use:&lt;/p&gt;

&lt;pre class="console"&gt;rake nhib21&lt;/pre&gt;

&lt;h2&gt;Updating trunk&lt;/h2&gt;

&lt;p&gt;The trunk of NHibernate is always going to be ahead of Fluent NHibernate, and we cannot keep it up to date; so the easiest way for you to do it yourself is to replace the contents of &lt;code&gt;tools/NHibernate/nhib2.1&lt;/code&gt; in your Fluent NHibernate directory. If you then run the above command for 2.1, it'll use your latest copy.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;There's a big but when using trunk, we don't officially support it; there probably will be some breaking changes in trunk at some point that we won't be able to accommodate for.&lt;/p&gt;
&lt;/blockquote&gt;</description>
      <pubDate>Sun, 29 Mar 2009 06:16:02 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/RakeScript</guid>
      <link>http://wiki.fluentnhibernate.org/show/RakeScript</link>
    </item>
    <item>
      <title>Standard Mapping Ids</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Fluent Mapping&lt;/strong&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/StandardMapping"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingClassMap"&gt;ClassMap&amp;lt;T&amp;gt;&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingIds"&gt;Ids&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingProperties"&gt;Properties&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingRelationships"&gt;Relationships&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingComponents"&gt;Components&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingConventions"&gt;Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingPrivateProperties"&gt;Private Properties&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;p&gt;If you haven't setup a Fluent NHibernate project before, you should checkout the &lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Introduction"&gt;GettingStarted: Introduction&lt;/a&gt; guide.&lt;/p&gt;

&lt;p&gt;As mentioned in &lt;a href="StandardMappingClassMap"&gt;ClassMap&lt;/a&gt;, all mappings are done from inside the constructor of a &lt;code&gt;ClassMap&amp;lt;T&amp;gt;&lt;/code&gt; derived class; so baring that in mind, all examples are going to &lt;em&gt;exclude&lt;/em&gt; the surrounding class.&lt;/p&gt;

&lt;h3&gt;Simple Ids&lt;/h3&gt;

&lt;p&gt;This is pretty much the standard &lt;code&gt;Id&lt;/code&gt; that most people use. Mapped as a &lt;code&gt;identity&lt;/code&gt; or &lt;code&gt;autoincrement&lt;/code&gt; in the database, and with a type of &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// schema
Id int identity(1, 1) primary key

// model
public int Id { get; private set; }

// mapping
Id(x =&gt; x.Id);&lt;/pre&gt;

&lt;p&gt;This mapping sees that the &lt;code&gt;Id&lt;/code&gt; property is an &lt;code&gt;int&lt;/code&gt; and makes the assumption that it's an auto-incrementing primary key.&lt;/p&gt;

&lt;p&gt;Similarly, if your &lt;code&gt;Id&lt;/code&gt; is a &lt;code&gt;Guid&lt;/code&gt;, then Fluent NHibernate uses the NHibernate &lt;code&gt;guid.comb&lt;/code&gt; generator, and a &lt;code&gt;string&lt;/code&gt; defaults to &lt;code&gt;assigned&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Overriding the column name&lt;/h3&gt;

&lt;p&gt;You may want to use a different name for the property in your model than what's in your database, here's how to map it:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// schema
some_ugly_id int identity(1, 1) primary key

// model
public int Id { get; private set; }

// mapping
Id(x =&gt; x.Id)
  .ColumnName("some_ugly_id");&lt;/pre&gt;

&lt;h3&gt;Specifying the generator&lt;/h3&gt;

&lt;p&gt;You may need to explicitly change what Fluent NHibernate uses as the identity generator; this could be if your &lt;code&gt;Id&lt;/code&gt; is an &lt;code&gt;int&lt;/code&gt; but you don't want it to be an auto-incrementing identity.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Id(x =&gt; x.Id)
  .GeneratedBy.Sequence("uuid_sequence");&lt;/pre&gt;

&lt;p&gt;You can use the &lt;code&gt;GeneratedBy&lt;/code&gt; property to specify various different generators for the &lt;code&gt;Id&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Currently &lt;code&gt;GeneratedBy&lt;/code&gt; supports: &lt;code&gt;Identity&lt;/code&gt;, &lt;code&gt;Increment&lt;/code&gt;, specifying a &lt;code&gt;Sequence&lt;/code&gt;, &lt;code&gt;HiLo&lt;/code&gt; and &lt;code&gt;SeqHiLo&lt;/code&gt;, &lt;code&gt;UuidHex&lt;/code&gt; and &lt;code&gt;UuidString&lt;/code&gt;, &lt;code&gt;Guid&lt;/code&gt; and &lt;code&gt;GuidComb&lt;/code&gt;, &lt;code&gt;Assigned&lt;/code&gt;, specifying a &lt;code&gt;Foreign&lt;/code&gt; identifier, and &lt;code&gt;Native&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An &lt;code&gt;Exception&lt;/code&gt; will be thrown if you try to use a generator with a type that isn't compatible, for example if you use &lt;code&gt;guid&lt;/code&gt; with an &lt;code&gt;int&lt;/code&gt; property.&lt;/p&gt;

&lt;h3&gt;Specifying unsaved values&lt;/h3&gt;

&lt;p&gt;If your domain stipulates that you must have a special unsaved value, then you can map it like so:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Id(x =&gt; x.Id)
  .WithUnsavedValue(-1);&lt;/pre&gt;

&lt;h3&gt;Specifying access strategies&lt;/h3&gt;

&lt;p&gt;If your entity uses a backing field for a property, or some other non-standard design, then you can map it using the &lt;code&gt;Access&lt;/code&gt; property.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// schema
Id int identity(1, 1) primary key

// model
private int _id;

public int Id
{
  get { return _id; }
}

// mapping
Id(x =&gt; x.Id)
  .Access.AsCamelCaseField();&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;Access&lt;/code&gt; property can be used to set various combinations of &lt;code&gt;Field&lt;/code&gt; and &lt;code&gt;Property&lt;/code&gt;, with various casings and prefixes.&lt;/p&gt;

&lt;p&gt;For example, for the same mapping but with the field called &lt;code&gt;_id&lt;/code&gt; you could use the &lt;code&gt;Prefix.Underscore&lt;/code&gt; overload: &lt;code&gt;Access.AsCamelCaseField(Prefix.Underscore)&lt;/code&gt;.&lt;/p&gt;</description>
      <pubDate>Fri, 27 Mar 2009 09:59:30 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/StandardMappingIds</guid>
      <link>http://wiki.fluentnhibernate.org/show/StandardMappingIds</link>
    </item>
    <item>
      <title>Home Page</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Navigation&lt;/strong&gt;&lt;br /&gt;
&lt;a href="https://fluentnhibernate.org"&gt;Main Site&lt;/a&gt;&lt;br /&gt;
&lt;a href="https://fluentnhibernate.org/issues"&gt;Issues&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://groups.google.com/group/fluent-nhibernate"&gt;Mailing List&lt;/a&gt;&lt;br /&gt;
&lt;a href="https://fluentnhibernate.org/downloads"&gt;Downloads&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Introduction"&gt;Getting Started guide&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/StandardMapping"&gt;Fluent Mapping&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Auto Mapping&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;p&gt;You've reached the official Wiki for &lt;a href="https://fluentnhibernate.org"&gt;Fluent NHibernate&lt;/a&gt;. You can use the right-hand menu to start browsing around.&lt;/p&gt;

&lt;p&gt;Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents (.hbm.xml files), Fluent NHibernate lets you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.&lt;/p&gt;

&lt;p&gt;Some good starting places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Introduction"&gt;Getting started guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="existingWikiWord" href="/show/StandardMapping"&gt;Fluent mapping&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Auto Mapping&lt;/a&gt; - where mappings are inferred from the design of your entities&lt;/li&gt;
&lt;li&gt;&lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Full application configuration with our &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="existingWikiWord" href="/show/DatabaseConfiguration"&gt;Database Configuration&lt;/a&gt; - fluently configure your database in code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just go &lt;a href="http://c2.com/cgi/wiki?WikiWalking"&gt;wiki-walking&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Any questions should be directed to the &lt;a href="http://groups.google.com/group/fluent-nhibernate"&gt;mailing list&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Tue, 24 Mar 2009 09:28:07 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/HomePage</guid>
      <link>http://wiki.fluentnhibernate.org/show/HomePage</link>
    </item>
    <item>
      <title>Auto Mapping Inheritance</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Auto Mapping&lt;/strong&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingComponents"&gt;Components&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingConventions"&gt;Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingTypeConventions"&gt;Type Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingAlteringEntities"&gt;Altering Entities&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingInheritance"&gt;Handling Inheritance&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingOverrides"&gt;Overriding Mappings&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingConfigurationAlterations"&gt;Configuration Alterations&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;p&gt;There are two main things that you'd want to do with inherited classes, either ignore the base class all together, or map them using an inheritance strategy. I'm going to start with the former, then move on to the latter.&lt;/p&gt;

&lt;h2&gt;Ignoring base-types&lt;/h2&gt;

&lt;p&gt;This scenario is where you may have a base class in your domain that you use to simplify your entities, you've moved common properties into it so you don't have to recreate them on every entity; typically this would be the Id and perhaps some audit information. So lets start with a model that has a base class we'd like to ignore.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
namespace Entities
{
  public abstract class Entity
  {
    public virtual int Id { get; set; }
  }

  public class Person : Entity
  {
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
  }

  public class Animal : Entity
  {
    public virtual string Species { get; set; }
  }
}&lt;/pre&gt;

&lt;p&gt;Relatively simple model here, we've got an &lt;code&gt;Entity&lt;/code&gt; base class that defines the &lt;code&gt;Id&lt;/code&gt;, then the &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Animal&lt;/code&gt; entities. We have no desire to have &lt;code&gt;Entity&lt;/code&gt; mapped by NHibernate, so we need a way to tell the auto mapper to ignore it.&lt;/p&gt;

&lt;p&gt;For those individuals from traditional XML mapping land, this is what we're going to be recreating:&lt;/p&gt;

&lt;pre name="code" class="xml"&gt;
&amp;lt;class name="Person"&amp;gt;
  &amp;lt;id name="Id" type="Int32"&amp;gt;
    &amp;lt;generator class="identity" /&amp;gt;
  &amp;lt;/id&amp;gt;

  &amp;lt;property name="FirstName" /&amp;gt;
  &amp;lt;property name="LastName" /&amp;gt;
&amp;lt;/class&amp;gt;

&amp;lt;class name="Animal"&amp;gt;
  &amp;lt;id name="Id" type="Int32"&amp;gt;
    &amp;lt;generator class="identity" /&amp;gt;
  &amp;lt;/id&amp;gt;

  &amp;lt;property name="Species" /&amp;gt;
&amp;lt;/class&amp;gt;&lt;/pre&gt;

&lt;p&gt;We need to initialise the NHibernate configuration, and supply it with any auto mappings we're going to create. To do that we'll combine the &lt;a href="AutoMapping"&gt;auto mapper&lt;/a&gt; with our &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration&lt;/a&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&amp;gt;
    m.AutoMappings.Add(
      AutoPersistenceModel  
        .MapEntitiesFromAssemblyOf&amp;lt;Entity&amp;gt;()  
        .Where(t =&gt; t.Namespace == "Entities")
    ))
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;The key part is inside the &lt;code&gt;AutoMappings.Add&lt;/code&gt; call, which is where we're telling the automapper to map anything in the &lt;code&gt;Entities&lt;/code&gt; namespace from the assembly that contains our &lt;code&gt;Entity&lt;/code&gt; base-class.&lt;/p&gt;

&lt;p&gt;If we were to run this now, we wouldn't get the mapping we desire. Fluent NHibernate would see &lt;code&gt;Entity&lt;/code&gt; as an actual entity and map it with &lt;code&gt;Animal&lt;/code&gt; and &lt;code&gt;Person&lt;/code&gt; as subclasses; this is not what we desire, so we need to modify our auto mapping configuration to reflect that.&lt;/p&gt;

&lt;p&gt;After &lt;code&gt;MapEntitiesFromAssemblyOf&amp;lt;Entity&amp;gt;()&lt;/code&gt; we need to alter the conventions that the auto mapper is using so it can identify our base-class.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&amp;gt;
    m.AutoMappings.Add(
      AutoPersistenceModel  
        .MapEntitiesFromAssemblyOf&amp;lt;Entity&amp;gt;()  
        .WithSetup(s =&amp;gt;
        {
          s.IsBaseType =
            type =&amp;gt; type == typeof(Entity);
        })
        .Where(t =&gt; t.Namespace == "Entities")
    ))
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;We've added the &lt;code&gt;WithSetup&lt;/code&gt; call in which we replace the &lt;code&gt;IsBaseType&lt;/code&gt; convention with our own. This convention is used to identify whether a type is simply a base-type for abstraction purposes, or a legitimate storage requirement. In our case we've set it to return &lt;code&gt;true&lt;/code&gt; if the type is an &lt;code&gt;Entity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With this change, we now get our desired mapping. &lt;code&gt;Entity&lt;/code&gt; is ignored as far is Fluent NHibernate is concerned, and all the properties (&lt;code&gt;Id&lt;/code&gt; in our case) are treated as if they were on the specific subclasses.&lt;/p&gt;

&lt;h2&gt;Base-type as an inheritance strategy&lt;/h2&gt;

&lt;p&gt;What we're going to do is create a simple model that we'll map as a [table-per-subclass][2]  inheritance strategy, which is the equivalent of the NHibernate &lt;code&gt;joined-subclass&lt;/code&gt; mapping.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
namespace Entities
{
  public class Person
  {
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
  }

  public class Employee : Person
  {
    public virtual DateTime StartDate { get; set; }
  }

  public class Guest : Person
  {
    public virtual int GuestPassId { get; set; }
  }
}&lt;/pre&gt;

&lt;p&gt;Relatively simple model here, we've got an &lt;code&gt;Person&lt;/code&gt; class that defines the &lt;code&gt;Id&lt;/code&gt; and name properties, then the &lt;code&gt;Employee&lt;/code&gt; and &lt;code&gt;Guest&lt;/code&gt; subclass entities.&lt;/p&gt;

&lt;p&gt;The XML equivalent is as follows:&lt;/p&gt;

&lt;pre name="code" class="xml"&gt;
&amp;lt;class name="Person"&amp;gt;
  &amp;lt;id name="Id" type="Int32"&amp;gt;
    &amp;lt;generator class="identity" /&amp;gt;
  &amp;lt;/id&amp;gt;

  &amp;lt;property name="FirstName" /&amp;gt;
  &amp;lt;property name="LastName" /&amp;gt;

  &amp;lt;joined-subclass name="Employee"&amp;gt;
    &amp;lt;key column="PersonId" /&amp;gt;
    &amp;lt;property name="StartDate" /&amp;gt;
  &amp;lt;/joined-subclass&amp;gt;

  &amp;lt;joined-subclass name="Guest"&amp;gt;
    &amp;lt;key column="PersonId" /&amp;gt;
    &amp;lt;property name="GuestPassId" /&amp;gt;
  &amp;lt;/joined-subclass&amp;gt;
&amp;lt;/class&amp;gt;&lt;/pre&gt;

&lt;p&gt;Again we configure NHibernate using the &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration&lt;/a&gt;:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&amp;gt;
    m.AutoMappings.Add(
      AutoPersistenceModel  
        .MapEntitiesFromAssemblyOf&amp;lt;Entity&amp;gt;()  
        .Where(t =&gt; t.Namespace == "Entities")
    ))
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;This is the same configuration that I used in the first example, except that if you recall the reason we had to change the last example was because it was mapping it as a joined-subclass - that's right, we don't need to do anything now! Our mapping is complete, Fluent NHibernate automatically assumes that any inherited classes (that haven't had their base-type excluded by the &lt;code&gt;IsBaseType&lt;/code&gt; convention) should be mapped as joined-subclasses.&lt;/p&gt;

&lt;p&gt;That's how to deal with base-classes with Fluent NHibernate's auto mapping&lt;/p&gt;</description>
      <pubDate>Sun, 22 Mar 2009 18:27:56 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/AutoMappingInheritance</guid>
      <link>http://wiki.fluentnhibernate.org/show/AutoMappingInheritance</link>
    </item>
    <item>
      <title>Auto Mapping Components</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Auto Mapping&lt;/strong&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingComponents"&gt;Components&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingConventions"&gt;Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingTypeConventions"&gt;Type Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingAlteringEntities"&gt;Altering Entities&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingInheritance"&gt;Handling Inheritance&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingOverrides"&gt;Overriding Mappings&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingConfigurationAlterations"&gt;Configuration Alterations&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;p&gt;The Fluent NHibernate AutoMapper is capable of automatically recognising simple components; currently only properties are supported, no relationships inside the components. With that in mind, I'll walk through how to automap your components. &lt;/p&gt;

&lt;p&gt;Lets imagine this database structure:&lt;/p&gt;

&lt;pre name="code"&gt;
table Person (
  Id int primary key,
  Name varchar(200),
  Address_Number int,
  Address_Street varchar(100),
  Address_PostCode varchar(8)
)&lt;/pre&gt;

&lt;p&gt;We want to map that to the following model:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class Person
{
  public virtual int Id { get; private set; }
  public virtual string Name { get; set; }
  public virtual Address Address { get; set; }
}

public class Address
{
  public int Number { get; set; }
  public string Street { get; set; }
  public string PostCode { get; set; }
}&lt;/pre&gt;

&lt;p&gt;With this design, &lt;code&gt;Address&lt;/code&gt; is actually a component, which isn't a full entity, more of a way of providing a clean model to a normalised database structure. I'll get started by setting up the auto-mapper with the &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration&lt;/a&gt; and &lt;a class="existingWikiWord" href="/show/DatabaseConfiguration"&gt;Database Configuration&lt;/a&gt; APIs.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
var sessionFactory = Fluently.Configure()
  .Database(MsSqlConfiguration.MsSql2005
    .ConnectionString(c =&gt; c
      .Is(ApplicationConnectionString)))
  .Mappings(m =&gt;
    m.AutoMappings.Add(
      AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;Person&amp;gt;()
        .Where(type =&gt; type.Namespace.EndsWith("Domain"))
  ))
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;We've now got the auto mappings integrated with NHibernate, so we need to instruct the auto mapper how to identify components; after the &lt;code&gt;Where&lt;/code&gt; call, we can add a call to &lt;code&gt;WithSetup&lt;/code&gt; to help the auto mapper identify our components.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.WithSetup(s =&gt;
{
  s.IsComponentType =
    type =&gt; type == typeof(Address);
})&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;IsComponentType&lt;/code&gt; convention is what Fluent NHibernate uses to determine whether a type is one that will be mapped as a component, rather than a full entity.&lt;/p&gt;

&lt;p&gt;There are two things you need to know about this convention:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can only set this convention once, so you'll need to find a way that allows you to identify multiple components; there are several options to this, including using the namespace (like &lt;code&gt;Where&lt;/code&gt; does), or checking a suffix on the type name (anything that ends in "Component", for example).&lt;/li&gt;
&lt;li&gt;This is not an exclusive call, so you need to segregate your component types from your standard entity types (so they'll be excluded by the &lt;code&gt;Where&lt;/code&gt; call), otherwise they'll be auto-mapped as full entities as well as components - &lt;em&gt;not good&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With that, the &lt;code&gt;Address&lt;/code&gt; should now be automatically mapped as a component; the auto mapper will pick up the three properties and map them as properties on the component.&lt;/p&gt;

&lt;p&gt;There's one more thing, for illustrative purposes I've deliberately gone against Fluent NHibernate's inbuilt convention for naming columns. By default any columns mapped in a convention will be prefixed with their type name, so &lt;code&gt;Address.Street&lt;/code&gt; would be mapped against a column called &lt;code&gt;AddressStreet&lt;/code&gt;; this is my personal preference, but not what our database contains! We can control how our columns are named by altering the &lt;code&gt;GetComponentColumnPrefix&lt;/code&gt; convention, like so:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.WithSetup(s =&gt;
{
  s.IsComponentType =
    type =&gt; type == typeof(Address);
  s.GetComponentColumnPrefix =
    type =&gt; type.Name + "_";
})&lt;/pre&gt;

&lt;p&gt;The convention now specifies that columns should be named TypeName_PropertyName, so &lt;code&gt;Address.Street&lt;/code&gt; is now correctly mapped against &lt;code&gt;Address_Street&lt;/code&gt;.&lt;/p&gt;</description>
      <pubDate>Thu, 19 Mar 2009 20:35:32 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/AutoMappingComponents</guid>
      <link>http://wiki.fluentnhibernate.org/show/AutoMappingComponents</link>
    </item>
    <item>
      <title>Standard Mapping Relationships</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Fluent Mapping&lt;/strong&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/StandardMapping"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingClassMap"&gt;ClassMap&amp;lt;T&amp;gt;&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingIds"&gt;Ids&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingProperties"&gt;Properties&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingRelationships"&gt;Relationships&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingComponents"&gt;Components&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingConventions"&gt;Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/StandardMappingPrivateProperties"&gt;Private Properties&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;p&gt;If you haven't setup a Fluent NHibernate project before, you should checkout the &lt;a class="existingWikiWord" href="/show/GettingStarted%3A+Introduction"&gt;GettingStarted: Introduction&lt;/a&gt; guide.&lt;/p&gt;

&lt;p&gt;As mentioned in &lt;a href="StandardMappingClassMap"&gt;ClassMap&lt;/a&gt;, all mappings are done from inside the constructor of a &lt;code&gt;ClassMap&amp;lt;T&amp;gt;&lt;/code&gt; derived class; so baring that in mind, all examples are going to &lt;em&gt;exclude&lt;/em&gt; the surrounding class.&lt;/p&gt;

&lt;p&gt;There are three relationships you can use to map between your entities, reference (many-to-one), has many (one-to-many), and has many to many (many-to-many); I'll start by discussing the exclusive features for each, then show the features that are common to both.&lt;/p&gt;

&lt;h2&gt;References / many-to-one&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;References&lt;/code&gt; method is used to create a &lt;code&gt;many-to-one&lt;/code&gt; relationship between two entities; that's where the current entity has a single property that is an instance of the other entity.&lt;/p&gt;

&lt;h3&gt;Simple reference&lt;/h3&gt;

&lt;p&gt;Here's a typical example of a &lt;code&gt;References&lt;/code&gt; usage.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// schema
table Account ( // our entity
  CustomerId int foreign key (Customer) references (Id)
)
table Customer (
  Id int identity(1,1) primary key
)

// model
public class Account
{
  public Customer Customer { get; set; }
}

// mapping
public AccountMap()
{
  References(x =&gt; x.Customer);
}&lt;/pre&gt;

&lt;p&gt;This creates a relationship between the two tables. In NHibernate hbm mapping, it's equivalent to &lt;code&gt;&amp;lt;many-to-one name="Customer" /&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Specifying a foreign key&lt;/h3&gt;

&lt;p&gt;If you're generating your schema from your mappings (see &lt;a class="existingWikiWord" href="/show/SchemaGeneration"&gt;Schema Generation&lt;/a&gt;), you may want to name your foreign key, you can do that using the &lt;code&gt;WithForeginKey&lt;/code&gt; method.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
References(x =&gt; x.Customer)
  .WithForeignKey();&lt;/pre&gt;

&lt;p&gt;This creates a foreign key based on the naming of your entities; if this example were an &lt;code&gt;Account&lt;/code&gt; that has a &lt;code&gt;Customer&lt;/code&gt;, the foreign key would be called &lt;code&gt;FK_Account_Customer&lt;/code&gt;. If you would prefer to specify your own key, you can use the &lt;code&gt;WithForeignKey(string)&lt;/code&gt; overload.&lt;/p&gt;

&lt;h3&gt;Specifying the column name&lt;/h3&gt;

&lt;p&gt;If your foreign key Id has a different column name than what you're using for a property, then you can use the &lt;code&gt;TheColumnNameIs&lt;/code&gt; method.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// schema
table Account ( // our entity
  Customer_RefId int foregin key (Customer) references (Id)
)
table Customer (
  Id int identity(1,1) primary key
)

// model
public Customer Customer { get; set; }

// mapping
References(x =&gt; x.Customer)
  .ColumnName("Customer_RefId");&lt;/pre&gt;

&lt;p&gt;If you need to specify multiple columns to use as a key, useful if the entity you're referencing has a composite-id, then you can use the &lt;code&gt;WithColumns&lt;/code&gt; method.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
References(x =&gt; x.Customer)
  .WithColumns("CustomerName", "CustomerReference");&lt;/pre&gt;

&lt;h3&gt;Changing the referenced column&lt;/h3&gt;

&lt;p&gt;If you need to link to the entity you're referencing by something other than it's primary key, then you can use the &lt;code&gt;PropertyRef&lt;/code&gt; method.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
References(x =&gt; x.Customer)
  .PropertyRef(x =&gt; x.CustomerReference);&lt;/pre&gt;

&lt;h3&gt;Handling null references&lt;/h3&gt;

&lt;p&gt;If it's allowed in your domain for a foreign key to be null, you may want to stipulate what behavior NHibernate should use on encountering a null; you can do this with the&lt;code&gt;NotFound&lt;/code&gt; property.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
References(x =&gt; x.Customer)
  .NotFound.Ignore();&lt;/pre&gt;

&lt;p&gt;The default behavior is &lt;code&gt;Exception&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;HasMany / one-to-many&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;HasMany&lt;/code&gt; method is used to create a &lt;code&gt;one-to-many&lt;/code&gt; relationship between two entities; that's where the current entity has a collection of instances of the other entity.&lt;/p&gt;

&lt;h3&gt;Simple HasMany&lt;/h3&gt;

&lt;p&gt;Here's a typical example of a &lt;code&gt;HasMany&lt;/code&gt; usage.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// schema
table Account ( // our entity
  Id int identity(1, 1) primary key
)
table Customer (
  AccountId int foreign key (Account) references (Id)
)

// model
public class Account
{
  public IList&amp;lt;Customer&amp;gt; Customers { get; private set; }

  public Account()
  {
    Customers = new List&amp;lt;Customer&amp;gt;();
  }
}

// mapping
public AccountMap()
{
  HasMany(x =&gt; x.Customers);
}&lt;/pre&gt;

&lt;p&gt;This creates a relationship between the two tables. In NHibernate hbm mapping, it's equivalent to:&lt;/p&gt;

&lt;pre name="code" class="xml"&gt;
&amp;lt;bag name="Customers"&amp;gt;
  &amp;lt;key column="AccountId" /&amp;gt;
  &amp;lt;one-to-many class="Account" /&amp;gt;
&amp;lt;/bag&amp;gt;&lt;/pre&gt;

&lt;h3&gt;Specifying the foreign key name&lt;/h3&gt;

&lt;p&gt;If you need to specify multiple columns to use as a key, useful if the entity you're referencing has a composite-id, then you can use the &lt;code&gt;WithKeyColumns&lt;/code&gt; method.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
HasMany(x =&gt; x.Customers)
  . WithKeyColumns("CustomerName", "CustomerReference");&lt;/pre&gt;

&lt;p&gt;Similarly, if you just want to specify one column name you can use &lt;code&gt;WithKeyColumn&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;HasManyToMany / many-to-many&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;HasManyToMany&lt;/code&gt; method is used to create a &lt;code&gt;many-to-many&lt;/code&gt; relationship between two entities; that's where there's a reference between two tables, with an intermediary table.&lt;/p&gt;

&lt;h3&gt;Simple HasManyToMany&lt;/h3&gt;

&lt;p&gt;Here's a typical example of a &lt;code&gt;HasManyToMany&lt;/code&gt; usage.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// schema
table Account ( // our entity
  Id int identity(1, 1) primary key
)
table AccountToCustomer (
  Account_id int foreign key (Account) references (Id)
  Customer_id int foreign key (Customer) references (Id)
)
table Customer (
  Id int identity(1, 1) primary key
)

// model
public class Account
{
  public IList&amp;lt;Customer&amp;gt; Customers { get; private set; }

  public Account()
  {
    Customers = new List&amp;lt;Customer&amp;gt;();
  }
}

// mapping
public AccountMap()
{
  HasManyToMany(x =&gt; x.Customers);
}&lt;/pre&gt;

&lt;p&gt;This creates a relationship between the two tables. In NHibernate hbm mapping, it's equivalent to:&lt;/p&gt;

&lt;pre name="code" class="xml"&gt;
&amp;lt;bag name="Customers"&amp;gt;
  &amp;lt;key column="Customer_id" /&amp;gt;
  &amp;lt;many-to-many class="Account" column="Account_id" /&amp;gt;
&amp;lt;/bag&amp;gt;&lt;/pre&gt;

&lt;h3&gt;Setting the table name&lt;/h3&gt;

&lt;p&gt;If your design has a different intermediary table name, you can use the &lt;code&gt;WithTableName&lt;/code&gt; method to set it.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
HasManyToMany(x =&amp;gt; x.Customers)
  .WithTableName("CustomerAddresses");&lt;/pre&gt;

&lt;h3&gt;Specifying the foreign key names&lt;/h3&gt;

&lt;p&gt;If you need to override the foreign key names that are used in the intermediary table, you can use &lt;code&gt;WithParentKeyColumn&lt;/code&gt; and &lt;code&gt;WithChildKeyColumn&lt;/code&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
table AccountToCustomer (
  Account_RefId int foreign key (Account) references (Id)
  Customer_RefId int foreign key (Customer) references (Id)
)

HasMany(x =&gt; x.Customers)
  .WithParentKeyColumn("Account_RefId")
  .WithChildKeyColumn("Customer_RefId");&lt;/pre&gt;

&lt;p&gt;You can use either of these methods, or both together.&lt;/p&gt;

&lt;h2&gt;Common settings for relationships&lt;/h2&gt;

&lt;p&gt;These settings are common to either all the relationships, or just the &lt;code&gt;HasMany&lt;/code&gt; and &lt;code&gt;HasManyToMany&lt;/code&gt;; I'll specify which relationships they're for.&lt;/p&gt;

&lt;p&gt;&lt;a name="specifying-access-strategies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Specifying access strategies&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;For all relationships&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If your entity uses a backing field for a property, or some other non-standard design, then you can map it using the &lt;code&gt;Access&lt;/code&gt; property.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;// model
public class Account
{
  private IList&amp;lt;Customer&amp;gt; customers = new List&amp;lt;Customer&amp;gt;();

  public IList&amp;lt;Customer&amp;gt; Customers
  {
    get { return customers; }
  }
}

// mapping
HasMany(x =&gt; x.Customers)
  .Access.AsCamelCaseField();&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;Access&lt;/code&gt; property can be used to set various combinations of &lt;code&gt;Field&lt;/code&gt; and &lt;code&gt;Property&lt;/code&gt;, with various casings and prefixes.&lt;/p&gt;

&lt;p&gt;For example, for the same mapping but with the field called &lt;code&gt;_customers&lt;/code&gt; you could use the &lt;code&gt;Prefix.Underscore&lt;/code&gt; overload: &lt;code&gt;Access.AsCamelCaseField(Prefix.Underscore)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Setting the fetch method&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;For all relationships&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you need to specify how joined entities are fetched, then you can use the &lt;code&gt;Fetch&lt;/code&gt; property.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
HasMany(x =&gt; x.Customers)
  .Fetch.Join();&lt;/pre&gt;

&lt;p&gt;The options are &lt;code&gt;Join&lt;/code&gt; and &lt;code&gt;Select&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Inverse&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;For all relationships&lt;/em&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
HasMany(x =&gt; x.Customers)
  .Inverse();&lt;/pre&gt;

&lt;p&gt;This sets the &lt;code&gt;inverse&lt;/code&gt; attribute.&lt;/p&gt;

&lt;h3&gt;Cascade&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;For all relationships&lt;/em&gt;&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
HasMany(x =&gt; x.Customers)
  .Cascade.All();&lt;/pre&gt;

&lt;p&gt;This sets the &lt;code&gt;cascade&lt;/code&gt; attribute; the options are &lt;code&gt;All&lt;/code&gt;, &lt;code&gt;None&lt;/code&gt;, &lt;code&gt;SaveUpdate&lt;/code&gt;, &lt;code&gt;Delete&lt;/code&gt;, and &lt;code&gt;AllDeleteOrphan&lt;/code&gt; (for &lt;code&gt;HasMany&lt;/code&gt; and &lt;code&gt;HasManyToMany&lt;/code&gt; only).&lt;/p&gt;

&lt;h3&gt;Setting the collection type&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;For &lt;code&gt;HasMany&lt;/code&gt; and &lt;code&gt;HasManyToMany&lt;/code&gt; only&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can override the type of collection that is used for relationships, you can read more about this in &lt;a class="existingWikiWord" href="/show/StandardMappingCollectionTypes"&gt;Standard Mapping Collection Types&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The default is to use &lt;code&gt;bag&lt;/code&gt;, but you can override it with the explicit methods.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
HasMany(x =&gt; x.Customers)
  .AsSet();&lt;/pre&gt;</description>
      <pubDate>Tue, 17 Mar 2009 21:30:39 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/StandardMappingRelationships</guid>
      <link>http://wiki.fluentnhibernate.org/show/StandardMappingRelationships</link>
    </item>
    <item>
      <title>Conventions Shortcuts</title>
      <description>&lt;p&gt;There are some shortcuts available for &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; that can be used inline (much like the old convention style); they're to be used for very simple situations where some people might think defining new types is overkill.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;Creating implementations of the &lt;a class="existingWikiWord" href="/show/ConventionsInterfaces"&gt;interface convention types&lt;/a&gt; is always the recommended approach, but if you think otherwise these are available.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;Builders&lt;/h2&gt;

&lt;p&gt;There is a &lt;code&gt;ConventionBuilder&lt;/code&gt; class that you can use to define simple conventions. The general rule-of-thumb is if your convention has any logic, or spans multiple lines, then don't use the builders. That being said, they can be useful for simple scenarios.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ConventionBuilder&lt;/code&gt; defines several static properties, one representing each &lt;a class="existingWikiWord" href="/show/ConventionsInterfaces"&gt;convention interface&lt;/a&gt;. Through each property you can access two methods, &lt;code&gt;Always&lt;/code&gt; and &lt;code&gt;When&lt;/code&gt;. These can be used to create simple conventions that are either always applied, or only applied in particular circumstances.&lt;/p&gt;

&lt;h3&gt;Always&lt;/h3&gt;

&lt;p&gt;These inline conventions are applied to every mapping instance of their type, they're handy for catch-all situations.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
ConventionBuilder.Class.Always(x =&gt; x.WithTable(x.EntityType.Name.ToLower()))
ConventionBuilder.Id.Always(x =&gt; x.ColumnName("ID"))&lt;/pre&gt;

&lt;h3&gt;When&lt;/h3&gt;

&lt;p&gt;These conventions are only applied when the first parameter evaluates to true.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
ConventionBuilder.Class.When(
  x =&gt; x.TableName == null, // when this is true
  x =&gt; x.WithTable(x.EntityType.Name + "Table") // do this
)&lt;/pre&gt;

&lt;h2&gt;Even shorter shortcuts&lt;/h2&gt;

&lt;p&gt;There are some situations that are so obvious that they just cried out for an even simpler shortcut.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Table.Is(x =&gt; x.EntityType.Name + "Table")
PrimaryKey.Name.Is(x =&gt; "ID")
DefaultLazy.AlwaysTrue()
DynamicInsert.AlwaysTrue()
DynamicUpdate.AlwaysTrue()
OptimisticLock.Is(x =&gt; x.Dirty())
Cache.Is(x =&gt; x.AsReadOnly())&lt;/pre&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;p&gt;To use these conventions you just need to call &lt;code&gt;Add&lt;/code&gt; on the &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration&lt;/a&gt; &lt;code&gt;ConventionDiscovery&lt;/code&gt; property.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Fluently.Configure()
  .Database(/* database config */)
  .Mappings(m =&amp;gt;
  {
    m.FluentMappings
      .AddFromAssemblyOf&amp;lt;Entity&amp;gt;()
      .ConventionDiscovery.Add(PrimaryKey.Name.Is(x =&gt; "ID"));
  })&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;Add&lt;/code&gt; method accepts a params array, so you can specify multiple conventions together.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.ConventionDiscovery.Add(
  PrimaryKey.Name.Is(x =&gt; "ID"),
  DefaultLazy.AlwaysTrue()
)&lt;/pre&gt;</description>
      <pubDate>Mon, 16 Mar 2009 14:40:28 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/ConventionsShortcuts</guid>
      <link>http://wiki.fluentnhibernate.org/show/ConventionsShortcuts</link>
    </item>
    <item>
      <title>Converting To New Style Conventions</title>
      <description>&lt;p&gt;So your existing conventions have broke with the introduction of the new convention style. I realise you're probably annoyed that your build is broken, but take a breath and make sure you've read &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; and &lt;a class="existingWikiWord" href="/show/ConventionsInterfaces"&gt;Conventions Interfaces&lt;/a&gt;. Once you've done that, if you still don't know what to do to fix your application, this might help.&lt;/p&gt;

&lt;h2&gt;Replacing existing conventions&lt;/h2&gt;

&lt;p&gt;Below are all the original conventions (or near enough) with their equivalent in the new-style. The logic change can be summarised as: old conventions were applied to everything, always; new style conventions are applied conditionally.&lt;/p&gt;

&lt;p&gt;I'll show some examples of updating your conventions after the table.&lt;/p&gt;

&lt;p&gt;&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Old Style&lt;/th&gt;
      &lt;th&gt;New Style&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;ITypeConvention&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IClassConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;IPropertyConvention&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Same but different method names&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.GetTableName&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IClassConvention&lt;/code&gt; or &lt;a href="/show/ConventionsShortcuts"&gt;Convention Shortcuts&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.GetPrimaryKeyName&lt;/code&gt; and &lt;code&gt;Conventions.GetPrimaryKeyNameFromType&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IIdConvention&lt;/code&gt; or &lt;a href="/show/ConventionsShortcuts"&gt;Convention Shortcuts&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.GetForeignKeyName&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IHasManyConvention&lt;/code&gt; or other related relationship convention&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.GetReadOnlyCollectionBackingFieldName&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IHasManyConvention&lt;/code&gt; or other related relationship convention&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.IdConvention&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IIdConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.OneToManyConvention&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IHasManyConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.ManyToOneConvention&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IReferenceConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.OneToOneConvention&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IHasOneConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.GetManyToManyTableName&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IHasManyToManyConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.JoinConvention&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IJoinConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.DefaultCache&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IClassConvention&lt;/code&gt; or &lt;a href="/show/ConventionsShortcuts"&gt;Convention Shortcuts&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.GetVersionColumnName&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IVersionConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.DefaultLazyLoad&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IClassConvention&lt;/code&gt; or &lt;a href="/show/ConventionsShortcuts"&gt;Convention Shortcuts&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.DynamicUpdate&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IClassConvention&lt;/code&gt; or &lt;a href="/show/ConventionsShortcuts"&gt;Convention Shortcuts&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.DynamicInsert&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IClassConvention&lt;/code&gt; or &lt;a href="/show/ConventionsShortcuts"&gt;Convention Shortcuts&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code&gt;Conventions.GetComponentColumnPrefix&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Implement an &lt;code&gt;IComponentConvention&lt;/code&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;&lt;/p&gt;

&lt;h2&gt;Examples: Converting old conventions&lt;/h2&gt;

&lt;p&gt;It goes without saying that implementing interfaces is going to be more verbose than using in-line lambdas, so prepare yourself because these conversions are going to end up being longer; however, the added separation makes for a much less coupled design. Learn to love it.&lt;/p&gt;

&lt;p&gt;You should read up on the &lt;a class="existingWikiWord" href="/show/Conventions"&gt;conventions in general&lt;/a&gt; and what &lt;a class="existingWikiWord" href="/show/ConventionsInterfaces"&gt;interfaces&lt;/a&gt; are available.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Primary Key naming&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You used to write the following to override the primary key naming to be TableNameId:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.WithConvention(c =&amp;gt;
  c.GetPrimaryKeyName = type =&amp;gt; type.Name + "Id");&lt;/pre&gt;

Now you'd implement an `IIdConvention` which allows you to alter the actual Id mapping itself.

&lt;pre name="code" class="c-sharp"&gt;
public class PrimaryKeyNamingConvention
  : IIdConvention
{
  public bool Accept(IIdentityPart id)
  {
    return true; // work on ALL ids
  }

  public void Apply(IIdentityPart id)
  {
    id.ColumnName(id.EntityType.Name + "Id");
  }
}&lt;/pre&gt;

That's your new convention, which you'd situate with all your other conventions, then use either of the following snippets to hook them in:

&lt;pre name="code" class="c-sharp"&gt;
.ConventionDiscovery.AddFromAssemblyOf&amp;lt;PrimaryKeyNamingConvention&amp;gt;();
.ConventionDiscovery.Add&amp;lt;PrimaryKeyNamingConvention&amp;gt;();
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Many-to-many table naming&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You used to write the following to override the table name used for many-to-many relationships:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.WithConvention(c =&amp;gt;
  c.GetManyToManyTableName = (child, parent) =&amp;gt; child.Name + "To" + parent.Name);&lt;/pre&gt;

Now you'd implement an `IHasManyToManyConvention` which allows you to alter the actual many-to-many mapping itself.

&lt;pre name="code" class="c-sharp"&gt;
public class ManyToManyTableNameConvention
  : IHasManyToManyConvention
{
  public bool Accept(IManyToManyPart relationship)
  {
    return true; // work on ALL ids
  }

  public void Apply(IManyToManyPart relationship)
  {
    relationship.WithTableName(relationship.ChildType.Name + "To" + relationship.EntityType.Name);
  }
}&lt;/pre&gt;

That's your new convention, which you'd situate with all your other conventions, then use either of the following snippets to hook them in:

&lt;pre name="code" class="c-sharp"&gt;
.ConventionDiscovery.AddFromAssemblyOf&amp;lt; ManyToManyTableNameConvention&amp;gt;();
.ConventionDiscovery.Add&amp;lt; ManyToManyTableNameConvention&amp;gt;();
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;A note on auto-mapping&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're using automapping, the conventions used to be mixed up together. Now anything that can be thought of configuring the automapper's discovery capabilities (for example &lt;code&gt;IsBaseType&lt;/code&gt;) is now available only under the &lt;code&gt;WithSetup&lt;/code&gt; method (which in-part replaces &lt;code&gt;WithConvention&lt;/code&gt;), everything else is a standard convention and is handled through the &lt;code&gt;ConventionDiscovery&lt;/code&gt; property like &lt;a class="existingWikiWord" href="/show/Conventions"&gt;the regular conventions&lt;/a&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
// old
.WithConvention(c =&amp;gt;
  c.IsBaseType = type =&amp;gt; type == typeof(BaseEntity));

// new
.WithSetup(s =&amp;gt;
  s.IsBaseType = type =&amp;gt; type == typeof(BaseEntity))
&lt;/pre&gt;

&lt;h2&gt;Attribute based conventions&lt;/h2&gt;

&lt;p&gt;If you were using the &lt;code&gt;ForAttribute&amp;lt;T&amp;gt;&lt;/code&gt; method, then you can inherit from the &lt;code&gt;AttributePropertyConvention&lt;/code&gt; abstract class.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class MyAttributeConvention
  : AttributePropertyConvention&amp;lt;MyCustomAttribute&amp;gt;
{
  public override void Apply(MyCustomAttribute attribute, IProperty property)
  {
    // your alterations
  }
}&lt;/pre&gt;</description>
      <pubDate>Fri, 13 Mar 2009 20:06:09 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/ConvertingToNewStyleConventions</guid>
      <link>http://wiki.fluentnhibernate.org/show/ConvertingToNewStyleConventions</link>
    </item>
    <item>
      <title>Standard Mapping Conventions</title>
      <description>&lt;p&gt;As with the &lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Auto Mapping&lt;/a&gt;, you can specify conventions to be used when your entities are pushed into NHibernate. I'll use the &lt;a class="existingWikiWord" href="/show/FluentConfiguration"&gt;Fluent Configuration&lt;/a&gt; API to illustrate how to set these conventions.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Fluent.Configuration()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&amp;gt;
    m.FluentMappings
      .AddFromAssemblyOf&amp;lt;Entity&amp;gt;&gt;()
      .ConventionDiscovery.AddFromAssemblyOf&amp;lt;MyConvention&amp;gt;())
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;You can read more on conventions in general on the &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; page.&lt;/p&gt;</description>
      <pubDate>Wed, 11 Mar 2009 20:45:10 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/StandardMappingConventions</guid>
      <link>http://wiki.fluentnhibernate.org/show/StandardMappingConventions</link>
    </item>
    <item>
      <title>Fluent Configuration</title>
      <description>&lt;p&gt;Fluent NHibernate provides an API for completely configuring NHibernate for use with your application, all within code. The API is broken down into five main methods, three of which are required.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
Fluently.Configure()
  .Database(/* your database settings */)
  .Mappings(/* your mappings */)
  .ExposeConfiguration(/* alter Configuration */) // optional
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;You can combine these methods in various ways to setup your application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Fluently.Configure&lt;/code&gt; starts the configuration process&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Database&lt;/code&gt; is where you specify your database configuration using the &lt;a class="existingWikiWord" href="/show/DatabaseConfiguration"&gt;Database Configuration&lt;/a&gt; api&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Mappings&lt;/code&gt; is where you supply which mappings you're using&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ExposeConfiguration&lt;/code&gt; is optional, but allows you to alter the raw Configuration object&lt;/li&gt;
&lt;li&gt;&lt;code&gt;BuildSessionFactory&lt;/code&gt; is the final call, and it creates the NHibernate SessionFactory instance from your configuration.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The API is fairly small, and relatively discoverable, so it's easy to configure it in a way that will work with your application; however, here are some common usages:&lt;/p&gt;

&lt;h2&gt;Exclusively fluent&lt;/h2&gt;

&lt;p&gt;If you're in the situation where your application is exclusively using fluent mappings, then this is the configuration for you. &lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&gt;
    m.FluentMappings
      .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;())
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;This setup uses the SQLite database configuration, but you can substitute that with your own; it then adds any fluent mappings from the assebly that contains &lt;code&gt;YourEntity&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Automappings&lt;/h2&gt;

&lt;p&gt;If you're using only auto mappings, then this config is for you.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&gt;
    m.AutoMappings.Add(
      // your automapping setup here
      AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;YourEntity&amp;gt;()
        .Where(type =&gt; type.Namspace.EndsWith("Entities"))))
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;Replace the code inside &lt;code&gt;AutoMappings.Add&lt;/code&gt; with your auto mapping configuration.&lt;/p&gt;

&lt;h2&gt;Mixed fluent mappings and auto mappings&lt;/h2&gt;

&lt;p&gt;If you're using a combination of standard fluent mappings and auto mappings, then this example should show you how to get started.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&gt;
  {
    m.FluentMappings
      .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;();

    m.AutoMappings.Add(
      // your automapping setup here
      AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;YourEntity&amp;gt;()
        .Where(type =&gt; type.Namspace.EndsWith("Entities")));
  })
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;You can see that this is a combination of the two previous examples, the &lt;code&gt;Mappings&lt;/code&gt; method can accept multiple kinds of mappings.&lt;/p&gt;

&lt;h2&gt;HBM mappings&lt;/h2&gt;

&lt;p&gt;You've not yet got around to using Fluent NHibernate fully, but you are configuring your database with it; this configuration will let you configure your database and add your traditional hbm mappings.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&gt;
    m.HbmMappings
      .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;())
  .BuildSessionFactory();&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;HbmMappings&lt;/code&gt; property allows you to add HBM XML mappings in a few different ways, this example adds everything from an assembly which defines &lt;code&gt;YourEntity&lt;/code&gt;; however, you can add from an assembly instance, or just add single types.&lt;/p&gt;

&lt;h2&gt;Mixed HBM and fluent mappings&lt;/h2&gt;

&lt;p&gt;You're migrating your entities to Fluent NHibernate but haven't quite got them all across yet - this is for you.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&gt;
  {
    m.HbmMappings
      .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;();

    m.FluentMappings
      .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;();
  })
  .BuildSessionFactory();&lt;/pre&gt;

&lt;h2&gt;The whole shebang: fluent, auto, and hbm mappings&lt;/h2&gt;

&lt;p&gt;You're a crazy fool and map a bit of everything, then this is how you'd configure it.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
var sessionFactory = Fluently.Configure()
  .Database(SQLiteConfiguration.Standard.InMemory)
  .Mappings(m =&gt;
  {
    m.HbmMappings
      .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;();

    m.FluentMappings
      .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;();

    m.AutoMappings.Add(
      // your automapping setup here
      AutoPersistenceModel.MapEntitiesFromAssemblyOf&amp;lt;YourEntity&amp;gt;()
        .Where(type =&gt; type.Namspace.EndsWith("Entities")));
  })
  .BuildSessionFactory();&lt;/pre&gt;

&lt;h2&gt;A couple of extras, for free!&lt;/h2&gt;

&lt;p&gt;Here's a few quick tricks people might appreciate.&lt;/p&gt;

&lt;h3&gt;Exporting hbm.xml mappings&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;Mappings&lt;/code&gt; call, you can do the following:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.Mappings(m =&gt;
{
  m.FluentMappings
    .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;()
    .ExportTo(@"C:\your\export\path");

  m.AutoMappings
    .Add(...)
    .ExportTo(@"C:\your\export\path");
})&lt;/pre&gt;

&lt;p&gt;That will export all of your fluent and automapped mappings in hbm.xml format to whatever location you specify.&lt;/p&gt;

&lt;h3&gt;Altering non-automapped conventions&lt;/h3&gt;

&lt;p&gt;If you want to override conventions that are used by your non-automapped classes, then you can use the &lt;code&gt;ConventionDiscovery&lt;/code&gt; property on &lt;code&gt;FluentMappings&lt;/code&gt;. This property exposes several methods which can be used to add &lt;a class="existingWikiWord" href="/show/Conventions"&gt;conventions&lt;/a&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.Mappings(m =&gt;
  m.FluentMappings
    .AddFromAssemblyOf&amp;lt;YourEntity&amp;gt;()
    .ConventionDiscovery.AddFromAssemblyOf&amp;lt;MyConvention&amp;gt;()&lt;/pre&gt;

&lt;p&gt;Some alternatives:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.ConventionDiscovery.Add&amp;lt;MyConvention&amp;gt;()
.ConventionDiscovery.Add(new MyConvention())
.ConventionDiscovery.AddFromAssemblyOf&amp;lt;MyConvention&amp;gt;()
.ConventionDiscovery.Setup(x =&gt;
{
  x.Add&amp;lt;MyConvention&amp;gt;();
  x.AddFromAssemblyOf&amp;lt;AnotherConvention&amp;gt;();
});&lt;/pre&gt;

&lt;h3&gt;Validation&lt;/h3&gt;

&lt;p&gt;If you forget to setup your database, or don't add any mappings, instead of pulling out your hair over obscure NHibernate exceptions, the &lt;code&gt;BuildSessionFactory&lt;/code&gt; method will throw a more helpful exception to try to point you in the right direction. It'll tell you whether you've forgot to add any entities, or not setup your database.&lt;/p&gt;</description>
      <pubDate>Wed, 11 Mar 2009 20:42:39 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/FluentConfiguration</guid>
      <link>http://wiki.fluentnhibernate.org/show/FluentConfiguration</link>
    </item>
    <item>
      <title>Auto Mapping Altering Entities</title>
      <description>&lt;div class="rightHandSide"&gt;

&lt;strong&gt;Auto Mapping&lt;/strong&gt;&lt;br /&gt;

&lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingComponents"&gt;Components&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingConventions"&gt;Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingTypeConventions"&gt;Type Conventions&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingAlteringEntities"&gt;Altering Entities&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingInheritance"&gt;Handling Inheritance&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingOverrides"&gt;Overriding Mappings&lt;/a&gt;&lt;br /&gt;
&lt;a class="existingWikiWord" href="/show/AutoMappingConfigurationAlterations"&gt;Configuration Alterations&lt;/a&gt;&lt;br /&gt;

&lt;/div&gt;

&lt;p&gt;Continuing with the same schema we used in &lt;a class="existingWikiWord" href="/show/AutoMappingTypeConventions"&gt;Auto Mapping Type Conventions&lt;/a&gt;, we now want to alter our &lt;strong&gt;has many&lt;/strong&gt; relationship from &lt;code&gt;Shelf&lt;/code&gt; to &lt;code&gt;Product&lt;/code&gt; so that it has a cascade set on it. We don't want this to affect all one-to-many's in our domain, so we need to do this alteration only on the &lt;code&gt;Shelf&lt;/code&gt; entity rather than with a &lt;a class="existingWikiWord" href="/show/Conventions"&gt;convention&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So how exactly do you supply alterations only for a specific entity? Easy! with &lt;code&gt;ForTypesThatDeriveFrom&amp;lt;T&amp;gt;(ClassMap&amp;lt;T&amp;gt;)&lt;/code&gt;.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.ForTypesThatDeriveFrom&amp;lt;Shelf&amp;gt;(map =&amp;gt;
{
  map.HasMany&amp;lt;Product&amp;gt;()
    .Cascade.All();
});&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;ForTypesThatDeriveFrom&lt;/code&gt; method takes a generic parameter that's the entity you want to customise. The parameter is an expression that allows you to alter the underlying &lt;code&gt;ClassMap&amp;lt;T&amp;gt;&lt;/code&gt; that is generated by the auto mapper. Anything you can do in the non-auto fluent mapping, you can do in this override. So for our case, we map a &lt;code&gt;HasMany&lt;/code&gt; of &lt;code&gt;Product&lt;/code&gt;, and specify it's cascade; this overrides the &lt;code&gt;HasMany&lt;/code&gt; that will have been generated by the auto mapper.&lt;/p&gt;

&lt;p&gt;That's it. We've overridden the auto mapping explicitly for a specific type, while not affecting the general conventions of the system. You can do this for as many types as you need in your domain; however, baring in mind readability, it may sometimes be more appropriate to map entities explicitly using the standard fluent mapping if you find yourself overriding a lot of conventions.&lt;/p&gt;</description>
      <pubDate>Wed, 11 Mar 2009 20:33:10 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/AutoMappingAlteringEntities</guid>
      <link>http://wiki.fluentnhibernate.org/show/AutoMappingAlteringEntities</link>
    </item>
    <item>
      <title>Auto Mapping Type Conventions</title>
      <description>&lt;p&gt;Now lets look at how you can do some deeper customisations of the &lt;a class="existingWikiWord" href="/show/AutoMapping"&gt;Auto Mapping&lt;/a&gt; tool.&lt;/p&gt;

&lt;p&gt;We're going to use the same domain as before, but with a few extensions.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class Product
{  
  public int Id { get; private set; }  
  public virtual string Name { get; set; }
  public virtual string Description { get; set; }
  public virtual decimal Price { get; set; }
  public virtual ReplenishmentDay ReplenishmentDay { get; set; }
}  

public class Shelf  
{  
  public virtual int Id { get; private set; }  
  public virtual IList&amp;lt;Product&amp;gt; Products { get; private set; }  
  public virtual string Description { get; set; }

  public Shelf()
  {
    Products = new List&amp;lt;Product&amp;gt;();
  }
}

public class ReplenishmentDay
{
  public static readonly ReplenishmentDay Monday = new ReplenishmentDay("mon");
  /* ... */
  public static readonly ReplenishmentDay Sunday = new ReplenishmentDay("sun");

  private string day;

  private ReplenishmentDay(string day)
  {
    this.day = day;
  }
}&lt;/pre&gt;

&lt;p&gt;We've extended our domain with a &lt;code&gt;Description&lt;/code&gt; and a &lt;code&gt;ReplenishmentDay&lt;/code&gt; for the &lt;code&gt;Product&lt;/code&gt;; the replenishment day is represented by a type-safe enum (using the &lt;a href="http://www.javacamp.org/designPattern/enum.html"&gt;type-safe enum pattern&lt;/a&gt;). Also there's a &lt;code&gt;Description&lt;/code&gt; against the &lt;code&gt;Shelf&lt;/code&gt; too (not sure why you'd have a description of a shelf, but hey, that's customers for you). These changes are mapped against the following schema:&lt;/p&gt;

&lt;pre&gt;
table Product (
  ProductId int identity primary key,
  Name varchar(250),
  Description varchar(2000),
  Price decimal,
  RepOn int, 
  Shelf_FK int foreign key
)

table Shelf (
  ShelfId int identity primary key,
  Description varchar(2000)
)&lt;/pre&gt;

&lt;p&gt;Now, if you've been following along you'll remember that we made all strings default to 250; and yet the new description columns are 2000 characters long. The customer has stipulated that all descriptions of anything in the domain will always be 2000 or less characters, so lets map that without affecting our other rule for strings.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.ConventionDiscovery.Setup(s =&gt;
{
  s.Add&amp;lt;DescriptionTypeConvention&amp;gt;();
  s.Add&amp;lt;DefaultStringLengthConvention&amp;gt;();

  // other conventions
});&lt;/pre&gt;

&lt;p&gt;We're using the Fluent NHibernate's &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; again, which allow you to override anything in the mappings before they're given to NHibernate. Baring in mind that our convention in this case is only for string properties that are called "Description", lets see how the &lt;code&gt;DescriptionTypeConvention&lt;/code&gt; is declared.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class DescriptionTypeConvention
  : IPropertyConvention
{
  public bool Accept(IProperty propertyMapping)
  {
    return propertyMapping.PropertyType == typeof(string) &amp;&amp;
      propertyMapping.Property.Name == "Description";
  }

  public void Apply(IProperty propertyMapping)
  {
    propertyMapping.WithLengthOf(2000);
  }
}&lt;/pre&gt;

&lt;p&gt;It's fairly expressive of what it does, but I'll cover it for completeness. The &lt;code&gt;IPropertyConvention&lt;/code&gt; specifies two methods: &lt;code&gt;bool Accept(IProperty)&lt;/code&gt; and &lt;code&gt;void Apply(IProperty)&lt;/code&gt;. &lt;code&gt;Accept&lt;/code&gt; should be implemented to return &lt;code&gt;true&lt;/code&gt; for properties that you want this convention to deal with; this can be handled in any way you want, you could check the name, or it's ancestry, but in our case we just check whether it's a string and if it has the correct name. &lt;code&gt;Apply&lt;/code&gt; is where the bulk of the work happens; this method gets called for every property mapping that has a type that &lt;code&gt;Accept&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt; for. We've implemented &lt;code&gt;Apply&lt;/code&gt; to alter the length of the property. Simple really. To learn more about conventions, see the &lt;a class="existingWikiWord" href="/show/Conventions"&gt;Conventions&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;With a simple implementation like this, we're able to map every Description property (that's a &lt;code&gt;string&lt;/code&gt;) so that it has a length of 2000, all with an addition of only one line to our auto mapping configuration.&lt;/p&gt;

&lt;h3&gt;IUserType support&lt;/h3&gt;

&lt;p&gt;The other alteration to our domain was the addition of the &lt;code&gt;ReplenishmentDay&lt;/code&gt;. There were two interesting things to consider for this change. Firstly, it's stored in an &lt;code&gt;int&lt;/code&gt; column, which obviously doesn't match our type; and secondly the column is called &lt;code&gt;RepOn&lt;/code&gt;, which we mustn't change. We're going to utilise NHibernate's &lt;code&gt;IUserType&lt;/code&gt; to handle this column.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;For the sake of this example we're going to assume you've got an &lt;code&gt;IUserType&lt;/code&gt; called &lt;code&gt;ReplenishmentDayUserType&lt;/code&gt;, but as it's beyond the scope of this post I won't actually show the implementation as it can be quite lengthy. It's best to just assume that the &lt;code&gt;IUserType&lt;/code&gt; reads an &lt;code&gt;int&lt;/code&gt; from the database and can convert it to a &lt;code&gt;ReplenishmentDay&lt;/code&gt; instance. There's a &lt;a href="http://intellect.dk/post/Implementing-custom-types-in-nHibernate.aspx"&gt;nice example of implementing &lt;code&gt;IUserType&lt;/code&gt;&lt;/a&gt; on &lt;a href="http://intellect.dk"&gt;Jakob Andersen&lt;/a&gt;'s blog.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So how do we tell Fluent NHibernate to use an &lt;code&gt;IUserType&lt;/code&gt; instead of the specified type? Easy, with another [[Conventions|convention], this time we'll use an &lt;code&gt;IUserTypeConvention&lt;/code&gt;, or more specifically we'll derive from the connivence base-class &lt;code&gt;UserTypeConvention&lt;/code&gt; that Fluent NHibernate supplies.&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
.ConventionDiscovery.Setup(s =&gt;
{
  s.Add&amp;lt;DescriptionTypeConvention&amp;gt;();
  s.Add&amp;lt;ReplenishmentDayTypeConvention&amp;gt;();
  s.Add&amp;lt;DefaultStringLength&amp;gt;();

  // other conventions
});&lt;/pre&gt;

&lt;p&gt;Here's how our new &lt;code&gt;ReplenishmentDayTypeConvention&lt;/code&gt; looks:&lt;/p&gt;

&lt;pre name="code" class="c-sharp"&gt;
public class ReplenishmentDayTypeConvention
  : UserTypeConvention&amp;lt;ReplenishmentDayUserType&amp;gt;
{
  public override void Apply(IProperty propertyMapping)
  {
    base.Apply(propertyMapping);

    propertyMapping.ColumnName("RepOn");
  }
}&lt;/pre&gt;

&lt;p&gt;The actual setting of the user type is handled by the base &lt;code&gt;Apply&lt;/code&gt; implementation, and if we weren't also setting the column name then we wouldn't need to override that member at all.&lt;/p&gt;

&lt;p&gt;So that's it, with those conventions we're able to keep our standard rule that all strings should be 250 characters or less, unless they're a Description, then they can be 2000 or less. Replenishment days use our type-safe enum, but are persisted to an &lt;code&gt;int&lt;/code&gt; in the database, which also has a custom column name.&lt;/p&gt;

&lt;p&gt;What next? &lt;a class="existingWikiWord" href="/show/AutoMappingAlteringEntities"&gt;Auto Mapping Altering Entities&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 11 Mar 2009 20:20:44 Z</pubDate>
      <guid>http://wiki.fluentnhibernate.org/show/AutoMappingTypeConventions</guid>
      <link>http://wiki.fluentnhibernate.org/show/AutoMappingTypeConventions</link>
    </item>
  </channel>
</rss>
