NHibernate参考不要'保存外键

时间:2011-10-13 13:40:20

标签: c# nhibernate fluent-nhibernate nhibernate-mapping

我有2个相互引用的类。这是我们CRM需要的一种奇怪的情况。

我有一个Organization和EmAddress表和类。组织继承自Subscriber,它也有一个表。我认为这可能是我的问题,或者我不能在这些上设置Inverse,因为没有“HasMany”......

插入/更新的顺序是.. 插入电子邮件 INSERT组织 更新电子邮件以设置Email.Subscriber

Email.Subscriber需要“NOT NULL”,所以这不起作用。如何更改顺序,我不能使用Inverse,因为没有列表。只有2个参考。

    public class Organization : Subscriber
    {
       public override string Class { get { return "Organization"; } }
       EmAddress PrimaryEmailAddress {get;set;}
    }

    public class OrganizationMap : SubclassMap<Organization>
    {        
      public OrganizationMap()
      {
        Table("Organization");
        KeyColumn("Organization");
        References(x => x.PrimaryEmail,"PrimaryEmailAddress").Cascade.SaveUpdate();
      }
    }

    public EmAddressMap()
    {
        Id(x => x.Id, "EmAddress");
        Map(x => x.EmailAddress, "eMailAddress");
        References<Subscriber>(x => x.Subscriber,"Subscriber").LazyLoad().Fetch.Select().Not.Nullable(); 
        /*not.nullable() throw s error.  NHibernate INSERTS email, INSERTS org, UPDATES email. */
   }

   public class EmAddress
   {
    public virtual Guid Id { get; set; }
    public virtual string EmailAddress { get; set; }
    public virtual Subscriber Subscriber { get; set; }
   }

     //Implementation
     var session = NHIbernateHelper.GetSession();
     using(var tx = session.BeginTransaction())
{
     var org = new Organization();
     org.PrimaryEmail = new EmAddress(){Subscriber = org};
     session.Save(org);
     tx.commit();

}

1 个答案:

答案 0 :(得分:0)

这篇文章可能有所帮助: http://ayende.com/blog/3960/nhibernate-mapping-one-to-one

只有一方使用多对一(流利:“参考”),另一方使用一对一(流利:“HasOne”)。