开发者

Nhibernate Bidirectional One-to-one (not HasOne)

开发者 https://www.devze.com 2023-04-06 14:26 出处:网络
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 whe开发者_如何转开发n you try to save

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 whe开发者_如何转开发n 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?


Try setting cascade="none" on one side of the Many-To-One mapping. Something like:

References(x=>x.C)
        .Not.Nullable()
        .Cascade.None();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号