title: DateTime precision in NHibernate and support for DateTime2 in NHibernate SchemeExport
body: <p>I am then using Fluent NHibernate and its automapping feature to map the the following simplified POCO class:</p>
  
  <pre><code>public class Foo
  {    
  public virtual int Id { get; set; }    
  public virtual datetime CreatedDateTime { get; set; }    
  }
  </code></pre>
  
  <p>The CreatedDateTime field will map to a SQL DateTime by default.  However if I do a test to check that the entity is being created correctly it fails.  This is because the precision of the DateTime field is not maintained through to the SQL database.  I undersatnd the reason behind this to be that a MS SQL Server DateTime can only hold milisecond precision by rounded to increments of .000, .003, or .007 (see <a href="http://msdn.microsoft.com/en-us/library/ms187819.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms187819.aspx</a>).  For this reason NHibernate truncates the miliseconds when saving to the store.  This results in my test failing when checking that the fields where persisted correctly as my .NET DateTime holds its miliseconds but the DateTime retrived after the save has lost its miliseconds and therefore the two are not truely equal.</p>
  
  <p>To overcome this problem I have added the following mapping to the Foo object:</p>
  
  <pre><code>public class FooMap : IAutoMappingOverride&lt;Foo&gt;
  {
      public void Override(AutoMapping&lt;Foo&gt; mapping)
      {
          mapping.Map(f =&gt; f.CreatedDateTime).CustomType("datetime2");     
      }
  }
  </code></pre>
  
  <p>I understand that this mapping makes NHibernate persist the CreatedDateTime to a SQL type of datetime2, which can store the full precision that a .NET DateTime can.
  This works a treat and the test now passes.</p>
  
  <p>However with one pass comes another fail: My test that checks the schema export now fails with the following error:</p>
  
  <pre><code>System.ArgumentException : Dialect does not support DbType.DateTime2
  Parameter name: typecode
  </code></pre>
  
  <p>with a stack trace of:</p>
  
  <pre><code>at NHibernate.Dialect.TypeNames.Get(DbType typecode)
  at NHibernate.Dialect.Dialect.GetTypeName(SqlType sqlType)
  at NHibernate.Mapping.Column.GetDialectTypeName(Dialect dialect, IMapping mapping)
  at NHibernate.Mapping.Table.SqlCreateString(Dialect dialect, IMapping p, String defaultCatalog, String defaultSchema)
  at NHibernate.Cfg.Configuration.GenerateSchemaCreationScript(Dialect dialect)
  at NHibernate.Tool.hbm2ddl.SchemaExport..ctor(Configuration cfg, IDictionary`2 configProperties)
  at NHibernate.Tool.hbm2ddl.SchemaExport..ctor(Configuration cfg)
  </code></pre>
  
  <p>The code uses the NHibernate.Tool.hbm2ddl.SchemaExport object to call the Execute method.</p>
  
  <p>I am using Fluent v1 and NHibernate v2.1.</p>
  
  <p>I have also tried mapping my <code>DateTime</code> to a TimeStamp but couldn't even get the mapping working as the insert fails stating:</p>
  
  <p>Cannot insert an explicit value into a timestamp column. Use <code>INSERT</code> with a column list to exclude the timestamp column, or insert a <code>DEFAULT</code> into the timestamp column.</p>
  
  <p>Does anyone know either how to get the SchemeExport working with a <code>datetime2</code> OR how to get timestamp mapping working for a <code>datetime</code> property?</p>