我应该如何正确使用 FluentNHibernate.Testing 的 CheckList?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么,但看来您确实需要使用
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 ofCheckList
. I'm not sure whatCheckList
does or if it works at all right now, but I looked at the SQL trace andCheckComponentList
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 overrideEquals
, so it is critical to either overrideEquals
in the child entity class or use one of theCheckComponentList
overloads that takes anIEqualityComparer
argument.我遇到了类似的问题,我的解决方案是避免设置“后”引用(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.