Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying datetime fluent nhibernate mappings

I'm encountering an issue when verifying the mappings on a very simple class.

System.ApplicationException : For property 'Created' expected same element, but got different element with the same value '8/9/2011 12:07:55 AM' of type 'System.DateTime'. Tip: use a CustomEqualityComparer when creating the PersistenceSpecification object.

I have tried creating overrides for the equals and get hashcode methods and that resulted in the same error. I dug into the custom equality comparer for persistence specification testing and again hit the same error. I should perhaps take a look at this with a fresh set of eyes in the morning but I feel i'm missing something very basic.

Thanks all.

public class Blah
{
    public int Id { get;  set; }
    public DateTime Created { get; set; }
    public string Description { get; set; }
}

[Test]
public void Can_Correctly_Map_Blah()
{
    new PersistenceSpecification<Blah>(Session)
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.Description, "Big Description")
        .CheckProperty(c => c.Created, System.DateTime.Now)
        .VerifyTheMappings();
}
like image 868
Jesse Avatar asked Aug 09 '11 06:08

Jesse


1 Answers

You have to be careful when comparing date times because it may seem like they are the same but they can vary down to the ticks (100 nanoseconds). It's probably failing because sql server doesn't store the date times that accurately.

You'll need use a custom equality comparer such that you only compare year, month, day, hour, minute and second probably.

Take a look at this article too: Why datetime cannot compare?

like image 67
Cole W Avatar answered Oct 18 '22 15:10

Cole W