3

I have a table with a datetime-type column as the version. It's a legacy DB so I can't possibly change it to datetime2 or use a different versioning mechanism. The NHibernate class is mapping this to a DateTime c# typed property.

I've seen several questions and also forum posts and replies regarding this issue, but regardless of what I've tried, NHibernate keeps truncating the milliseconds off the DateTime value.

Here's what I'm currently doing with Fluent NHibernate:

Version(x => x.ModifiedOn).Column("ModifiedOn")
                          .CustomType("Timestamp").Not.Nullable();

And in my class that's being mapped, I have:

public virtual System.DateTime ModifiedOn { get; set; }

The database is MS SQL 2008, and Fluent NH is configured as such:

Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008.ConnectionString......)

What I need: a working example of how to configure Fluent NH to make NH send datetime values with milliseconds (from what I've seen in the code it should be 10 ms accuracy). Thanks!

sinelaw
  • 16,205
  • 3
  • 49
  • 80
  • Just a thought.. can you let SQL Server generate the? Also, SQL Server is accurate to 3.33 milliseconds (1/300) – gbn Jul 03 '11 at 19:42
  • gbn: Thanks, well it's possible, but not the solution I'm looking for - I don't want to fiddle with the database. About the 3.33ms resolution - I know about it, and am willing to settle for less (10ms is what NH is supposed to support) but not one whole second! – sinelaw Jul 03 '11 at 19:55
  • Does my found solution to this question help at all? http://stackoverflow.com/questions/5330554/sqlite-wont-match-c-datetime-retrieved-from-database – Kevin Stricker Jul 03 '11 at 21:11
  • mootinator: I understand you're storing the DateTime as a string in the database, but my (legacy) database is defined with datetime columns and I can't change it. – sinelaw Jul 04 '11 at 17:39
  • For those interested, we've decided that this will be our last version using NHibernate. Seems like the project is not so active any more and the explosion in API and features does more harm than good. We'll try going back to EF. – sinelaw Jul 17 '11 at 10:17
  • 1
    "project is not so active any more" and "explosion in API and features" seem contradictory to me. – Daniel Schilling Aug 04 '11 at 17:47
  • Daniel, what I meant in "explosion" was that since NHibernate exists for a long time, there are many different interfaces some of which overlap (criteria / linq, SaveOrUpdate / Merge, and many more examples), and on the other hand it doesn't seem like there's a massive cleanup going on - in fact it doesn't seem like ANYTHING is going on in the last few months, which makes the project look inactive. – sinelaw Aug 10 '11 at 00:12
  • Sorry - my comment was a tad instigative. I'm a big fan of NHibernate and hate to see anyone leave it and go over to the EF dark side. Yes, there are a wide variety of NHibernate querying API's (HQL, Criteria, LINQ, and my favorite - QueryOver), but I see that as a plus. I've been using NHibernate since v1.2.1, and have loved every new release. See http://sourceforge.net/projects/nhibernate/files/NHibernate/ for an overview of the activity you've been missing. – Daniel Schilling Aug 12 '11 at 02:56

1 Answers1

0

The behavior you're describing (truncating milliseconds) is NHibernate's default behavior for DateTime values, so it sounds as if Fluent NHibernate is ignoring the .CustomType("Timestamp"). Could you please export the mapping files and post one of the *.hbm.xml files here? That will help us to determine whether the problem is in NHibernate, Fluent NHibernate, or possibly somewhere else. See the Fluent NHibernate documentation for instructions on exporting the mapping files.

If Fluent NHibernate is working correctly, then you should see something similar to

<version name="ModifiedOn" type="Timestamp">
  <column name="ModifiedOn" not-null="true" />
</version>

I think instead you'll see type="System.DateTime". The most likely cause is that your Fluent NHibernate code is simply not being executed. Posting details about how you use Fluent NHibernate to create your session factory would help us determine whether that's the cause or not.

Daniel Schilling
  • 4,829
  • 28
  • 60
  • Seems to work now with `.CustomType("Timestamp")`. I'm using Fluent NHibernate nuget package version 2.0.1.0. Change it in our code bases to include the custom type in the maps and now see the milliseconds coming through. (SQL Server version is 2012 R2). – Manfred Sep 11 '15 at 00:28