我应该如何正确使用 FluentNHibernate.Testing 的 CheckList?

发布于 2024-11-19 07:33:04 字数 997 浏览 3 评论 0原文

我正在尝试使用 FNH 中内置的持久性规范测试CheckList 方法似乎是最近添加的,但较旧的 CheckEnumerable 已被弃用,因此我假设 CheckList 是稳定的。

我的测试代码看起来与此类似:

new PersistenceSpecification<Parent>(session)
    .CheckProperty(x => x.Foo, 123)
    .CheckList(x => x.Children,
        new Child[] { new Child { Name = "Bob" } },
        (p, c) =>
        {
            p.Children.Add(c);
            c.Parent = p;
        })
    .VerifyTheMappings();

请注意,在映射中,Parent 拥有该关系(具有 Cascade.AllDeleteOrphan() 并且)。

当我运行它时,我收到可怕的“Cannot insert NULL value into...”SQL Server 错误消息,因为 NHibernate 没有在 ParentId 上设置>子实体。与忘记在两端设置关联时发生的情况相同。

我检查了断点,并且 lambda 内部的代码甚至没有被执行,这显然就是关联设置不正确的原因。

映射本身是完全正确的;我可以编写普通代码来创建和插入实体就可以了。只是我无法使用 CheckList 方法。

我做错了什么?

I'm trying to use the persistence specification testing built into FNH. The CheckList method seems to have been added recently, but the older CheckEnumerable has already been deprecated, so I'm assuming that CheckList is stable.

My test code looks similar to this:

new PersistenceSpecification<Parent>(session)
    .CheckProperty(x => x.Foo, 123)
    .CheckList(x => x.Children,
        new Child[] { new Child { Name = "Bob" } },
        (p, c) =>
        {
            p.Children.Add(c);
            c.Parent = p;
        })
    .VerifyTheMappings();

Note that in the mapping, the Parent owns the relationship (has Cascade.AllDeleteOrphan() and doesn't have Inverse).

When I run it, I get the dreaded "Cannot insert NULL value into..." SQL Server error message, because NHibernate isn't setting the ParentId on the Child entity. Same as what happens when you forget to set up the association on both ends.

I checked with a breakpoint and the code inside the lambda isn't even getting executed, which is obviously why the association isn't being set properly.

The mappings themselves are totally correct; I can write ordinary code to create and insert an entity just fine. It's just the CheckList method that I can't get to work.

What am I doing wrong?

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

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

发布评论

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

评论(2

无声静候 2024-11-26 07:33:04

我不知道为什么,但看来您确实需要使用 CheckComponentList 而不是 CheckList。我不确定 CheckList 做了什么,或者它现在是否正常工作,但我查看了 SQL 跟踪,并且 CheckComponentList 正在生成正确的语句。

CheckComponentList 使用默认的相等比较器,除非明确指定,这是不重写 Equals 的引用类型的引用相等,因此重写 子实体类中的 Equals 或使用采用 IEqualityComparer 参数的 CheckComponentList 重载之一。

I'm not sure why, but it appears that you do need to use CheckComponentList instead of CheckList. I'm not sure what CheckList does or if it works at all right now, but I looked at the SQL trace and CheckComponentList was generating the correct statements.

CheckComponentList uses the default equality comparer unless one is explicitly specified, which is reference-equality for reference types that do not override Equals, so it is critical to either override Equals in the child entity class or use one of the CheckComponentList overloads that takes an IEqualityComparer argument.

疏忽 2024-11-26 07:33:04

我遇到了类似的问题,我的解决方案是避免设置“后”引用(c.Parent = p)。我不知道这是否能解决你的问题,但你可以尝试一下。

I had a similar problem and my solution was to avoid setting the "back" reference (c.Parent = p). I don't know if that fixes your problem, but you can try it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文