我的域名:流利NHibnerate域映射問題
public class Person
{
public Person() { }
public virtual int PersonId { get; set; }
public virtual string Title { get; set; }
public virtual string FirstName { get; set; }
public virtual IList<Address> Addresses { get; set; }
}
public class Address
{
public Address()
{}
public virtual int AddressId { get; set; }
public virtual Person AddressPerson { get; set; }
public virtual string BuildingNumber { get; set; }
public virtual string AddressLine1 { get; set; }
}
我的映射:
public class AddressMap : ClassMap<Address>
{
public AddressMap()
{
Table("Address");
LazyLoad();
Id(x => x.AddressId).GeneratedBy.Identity();
References(x => x.AddressPerson).Column("PersonId").Not.Nullable();
Map(x => x.BuildingNumber).Length(250).Not.Nullable();
Map(x => x.AddressLine1).Length(100).Not.Nullable();
}
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Table("Person");
LazyLoad();
Id(x => x.PersonId).Column("PersonId").GeneratedBy.Identity();
Map(x => x.Title).Length(6).Nullable();
Map(x => x.FirstName).Length(100).Not.Nullable();
HasMany(x => x.Addresses).KeyColumn("PersonId");
HasMany(x => x.Applications).KeyColumn("PersonId");
}
}
所以,當我嘗試將地址添加到個人名單,並保存我收到以下錯誤:
object references an unsaved transient instance - save the transient instance before flushing. Type: Rise.Core.Domain.Address, Entity: Rise.Core.Domain.Address
我是NHibernate的新手,對於究竟發生了什麼,我有點困惑。我相信我需要創建一個雙向屬性或者我應該在保存人員ID session.SaveOrUpdate(Person)和session.SaveOrUpdate(Address)之後自己保存地址?不知道我究竟做了什麼錯誤,我確實喜歡在Person上可以延遲加載的地址列表,因爲它可以很容易地編寫一些Linq。
有什麼建議嗎?
Google'nhibernate cascading transient'。有同樣的問題,但目前沒有時間寫詳細的答案。 – 2010-09-10 15:54:51