NHibernate引用不要'保存外键
本文关键字:保存 引用 NHibernate | 更新日期: 2023-09-27 18:11:05
我有两个相互引用的类。这是一个奇怪的情况,我们的CRM需要。
我有一个组织和EmAddress表和类。组织从订阅服务器继承,订阅服务器也有一个表。我认为这可能是我的问题,或者我不能在这些上设置逆,因为没有"HasMany"…
插入/更新的顺序是…插入电子邮件插入组织UPDATE Email设置Email。订户
电子邮件。订阅者需要为"NOT NULL",所以这不起作用。我怎么能改变顺序,我不能用逆,因为没有列表。只有两个参考
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();
}
这篇文章可能会有所帮助:http://ayende.com/blog/3960/nhibernate-mapping-one-to-one
只有一方使用多对一(Fluent: "References"),另一方使用一对一(Fluent: "HasOne")。