NHibernate configuration for uni-directional one-to-many relation

后端 未结 3 795
生来不讨喜
生来不讨喜 2020-12-01 08:07

I\'m trying to set up a relationship as follows. Each Master item has one or more Detail items:

public class Detail {
    p         


        
3条回答
  •  情歌与酒
    2020-12-01 09:04

    NH3 and above allow to correct save entities in case of uni-directional one-to-many mapping without annoying save null-save-update cycle, if you set both not-null="true" on and inverse="false" on

    FluentNHibernate code snippet for that:

    public class MasterMap : ClassMap 
    {
        public MasterMap() 
        {
            Id(x => x.MasterId);
            Map(x => x.Name);
            HasMany(x => x.Details)
                .Not.Inverse()     //these options are very
                .Not.KeyNullable() //important and work only if set together 
                .Not.KeyUpdate()   //to prevent double update
                .Cascade.All();
        }
    }
    

提交回复
热议问题