验证日期时间流畅的 nhibernate 映射

发布于 2024-11-28 17:48:57 字数 856 浏览 1 评论 0原文

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

System.ApplicationException:对于属性“Created”,预期相同 元素,但得到具有相同值的不同元素'8/9/2011 12:07:55 AM”,类型为“System.DateTime”。提示:使用 创建 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似狗非友 2024-12-05 17:48:57

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

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

也看看这篇文章:
为什么日期时间无法比较?

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?

_畞蕅 2024-12-05 17:48:57

我刚刚在使用内存 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))

I just ran into this while using an in-memory SQLite session. I debugged through it and noticed that the DateTimes' "Milliseconds" and "Kind" properties differed ("Utc" Kind vs. "Unspecified").

My implementation per Cole W's suggestion:

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

Your specification test becomes something like this:

var maxDifference = TimeSpan.FromSeconds(1);
...
new PersistenceSpecification<Blah>(Session)
    ...
    .CheckProperty(c => c.Created, System.DateTime.Now,
            new DateTimeEqualityComparer(maxDifference))
一念一轮回 2024-12-05 17:48:57

简单的解决方案是创建 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();
}

Simple solution is to create a new instance of the 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文