我的域名:流利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