Nhibernate 双向一对一(不是 HasOne)
我试图在两个类之间建立一对一的关系(双向引用关系)。这两个属性都不应该为空。问题是当您尝试先保存一个而不是另一个时,我遇到空值或瞬态值错误。
class A
{
C C {get;set;}
}
class C
{
A A {get;set;}
}
class AMapping : ClassMap<A>
{
AMapping()
{
References(x=>x.C)
.Not.Nullable();
}
}
class CMapping : ClassMap<C>
{
CMapping()
{
References(x=>x.A)
.Not.Nullable();
}
}
我知道,由于 NHibernate 无法引用尚不存在(在数据库中)的对象,但是是否有一种模式或功能可以用来规避此限制?
I'm trying to establish a One-to-One relationship (Two-sided References relationship) between two classes. Both Properties should not be nullable. The problem is when you try to save one first over the other, I encounter the Null or Transient value error.
class A
{
C C {get;set;}
}
class C
{
A A {get;set;}
}
class AMapping : ClassMap<A>
{
AMapping()
{
References(x=>x.C)
.Not.Nullable();
}
}
class CMapping : ClassMap<C>
{
CMapping()
{
References(x=>x.A)
.Not.Nullable();
}
}
I understand that since NHibernate can't make a reference to an object that doesn't exist (in the databaase) yet, but would there be a pattern or a feature that I can use to circumvent this limitation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在多对一映射的一侧设置cascade="none"。像这样的东西:
Try setting cascade="none" on one side of the Many-To-One mapping. Something like: