【问题标题】:Verifying datetime fluent nhibernate mappings验证日期时间流利的休眠映射
【发布时间】:2011-08-09 06:16:10
【问题描述】:

我在验证一个非常简单的类的映射时遇到了问题。

System.ApplicationException:对于属性“已创建”预期相同 元素,但得到了具有相同值 '8/9/2011 的不同元素 'System.DateTime' 类型的 12:07:55 AM'。提示:使用 创建 PersistenceSpecification 时的 CustomEqualityComparer 对象。

我已尝试为 equals 和 get hashcode 方法创建覆盖,这导致了同样的错误。我深入研究了自定义相等比较器以进行持久性规范测试,并再次遇到相同的错误。我也许应该在早上用全新的眼光来看看这个,但我觉得我错过了一些非常基本的东西。

谢谢大家。

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();
}

【问题讨论】:

    标签: c# fluent-nhibernate


    【解决方案1】:

    在比较日期时间时必须小心,因为它们可能看起来相同,但它们可能会变化到滴答声(100 纳秒)。它可能会失败,因为 sql server 没有准确地存储日期时间。

    您需要使用自定义相等比较器,以便您可能只比较年、月、日、小时、分钟和秒。

    也看看这篇文章: Why datetime cannot compare?

    【讨论】:

    • 谢谢 Cole,我是新人,我昨晚应该上床睡觉,早上用全新的视角看这个。
    【解决方案2】:

    我刚刚在使用内存中的 SQLite 会话时遇到了这个问题。我通过它进行调试并注意到 DateTimes 的“毫秒”和“种类”属性不同(“Utc”种类与“未指定”)。

    我根据 Cole W 的建议实施:

    class DateTimeEqualityComparer : IEqualityComparer
    {
        private TimeSpan maxDifference;
    
        public DateTimeEqualityComparer(TimeSpan maxDifference)
        {
            this.maxDifference = maxDifference;
        }
    
        public bool Equals(object x, object y)
        {
            if (x == null || y == null)
            {
                return false;
            }
            else if (x is DateTime && y is DateTime)
            {
                var dt1 = (DateTime)x;
                var dt2 = (DateTime)y;
                var duration = (dt1 - dt2).Duration();
                return duration < maxDifference;
            }
            return x.Equals(y);
        }
    
        public int GetHashCode(object obj)
        {
            throw new NotImplementedException();
        }
    }
    

    你的规范测试变成这样:

    var maxDifference = TimeSpan.FromSeconds(1);
    ...
    new PersistenceSpecification<Blah>(Session)
        ...
        .CheckProperty(c => c.Created, System.DateTime.Now,
                new DateTimeEqualityComparer(maxDifference))
    

    【讨论】:

      【解决方案3】:

      简单的解决方案是创建一个新的 DateTime 实例

      [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,  new DateTime(2016, 7, 15, 3, 15, 0) )
              .VerifyTheMappings();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 2020-05-15
        • 1970-01-01
        • 2010-09-18
        • 2011-11-15
        相关资源
        最近更新 更多