title: Issue With Fluent Nhibernate Automapping and Guids / UniqueIdentifiers as Primary Key Fields
body: <p>I am attempting to use the Fluent-NHibernate automapping functionality (in the latest version of the software) and am running into problems using Guids as the Primary Key fields. If I use integer fields for the primary keys, the tables are generated successfully and all Nhibernate functionality seems to work fine. FYI, I am using NHibernate to generate my database tables.</p>
  
  <p>Here are a couple of classes with integer IDs.</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  using System.Reflection;
  
  namespace Sample.Data.Entities
  {
      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; }
      }
  
      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&lt;Store&gt; StoresStockedIn { get; private set; }
  
          public Product()
          {
              StoresStockedIn = new List&lt;Store&gt;();
          }
      }
  
      public class Store
      {
          public virtual int Id { get; private set; }
          public virtual string Name { get; set; }
          public virtual IList&lt;Product&gt; Products { get; set; }
          public virtual IList&lt;Employee&gt; Staff { get; set; }
  
          public Store()
          {
              Products = new List&lt;Product&gt;();
              Staff = new List&lt;Employee&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);
          }
      }
  }
  </code></pre>
  
  <p>Here are the same classes with GUIDs.</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  using System.Reflection;
  
  namespace Sample.Data.Entities
  {
      public class Employee
      {
          public virtual Guid Id { get; private set; }
          public virtual string FirstName { get; set; }
          public virtual string LastName { get; set; }
          public virtual Store Store { get; set; }
      }
  
      public class Product
      {
          public virtual Guid Id { get; private set; }
          public virtual string Name { get; set; }
          public virtual double Price { get; set; }
          public virtual IList&lt;Store&gt; StoresStockedIn { get; private set; }
  
          public Product()
          {
              StoresStockedIn = new List&lt;Store&gt;();
          }
      }
  
      public class Store
      {
          public virtual Guid Id { get; private set; }
          public virtual string Name { get; set; }
          public virtual IList&lt;Product&gt; Products { get; set; }
          public virtual IList&lt;Employee&gt; Staff { get; set; }
  
          public Store()
          {
              Products = new List&lt;Product&gt;();
              Staff = new List&lt;Employee&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);
          }
      }
  }
  </code></pre>
  
  <p>Here is my configuration.</p>
  
  <pre><code>    return Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008
        .ConnectionString(c =&gt; c.FromConnectionStringWithKey("AAAConnectionString"))
        .UseReflectionOptimizer()              
        .AdoNetBatchSize(25)
        .DefaultSchema("dbo")
        .Cache(c =&gt; c
          .UseQueryCache()
          .ProviderClass&lt;HashtableCacheProvider&gt;())
        .ShowSql())
        .Mappings(m=&gt;m.AutoMappings
          .Add(AutoMap.AssemblyOf&lt;Sample.Data.Entities.Product&gt;()                
          .Where(type =&gt; type.Namespace == "Sample.Data.Entities.Product")
          .Conventions.AddFromAssemblyOf&lt;Sample.Data.Fluent.Conventions.PrimaryKeyNameConvention&gt;()
          ))
        .ExposeConfiguration(BuildSchema)              
        .BuildSessionFactory();
  </code></pre>
  
  <p>To work around the issue, I attempted to generate conventions (see below) for 1) naming the Id field (although I thought it should have been unnecessary) and for 2) generating the Id (which I thought would have been automatic). I am unsure what is happening or why this is not working.</p>
  
  <pre><code>public class PrimaryKeyNameConvention : IIdConvention
  {
      public bool Accept(IIdentityInstance id)
      {
          return true;
      }
      public void Apply(IIdentityInstance id)
      {
          id.Column("Id");                
      }
  }
  
  public class PrimaryKeyGeneratorConvention : IIdConvention
  {
      public bool Accept(IIdentityInstance id)
      {
          return true;
      }
      public void Apply(IIdentityInstance id)
      {
          id.GeneratedBy.GuidComb();
      }
  }
  </code></pre>
  
  <p>Also, if I turn automapping off and use Fluently configured map the tables are generated successfully.</p>
  
  <p>This is driving me nuts, and I am sure it is probably a quick fix. Any ideas? </p>
  
  <p>Thank you!</p>
  
  <p>Anthony</p>