This是我的数据库项目的片段,并this是产生DB汽车的片段,该功能NHibernate非空属性引用null或瞬时值 - 逆的理解和级联
这些都是我的实体和映射类在上面的DB项目的Fluent NHibernate中。
实体:
public class BaseEntity<T> where T : BaseEntity<T> {
public virtual int Id { get; set; }
...
}
public class Person<T> : BaseEntity<T> where T : BaseEntity<T> {
public virtual string FirstName { get; set; }
public virtual string SecondName { get; set; }
public virtual string LastName { get; set; }
public virtual string PESEL { get; set; }
public virtual DateTime BirthDate { get; set; }
public virtual string City { get; set; }
public virtual string PostalCode { get; set; }
public virtual string Street { get; set; }
public virtual string HouseNr { get; set; }
public virtual string ApartmentNr { get; set; }
public virtual string PhoneNr { get; set; }
public virtual string Email { get; set; }
}
public class DrivingLicense : BaseEntity<DrivingLicense> {
#region Relations
public virtual IList<DrivingLicensePermissions> DrivingLicensePermissions { get; set; }
public virtual Student Student { get; set; }
#endregion
public virtual DateTime IssueDate { get; set; }
public virtual string DrivingLicenseNr { get; set; }
}
public class DrivingLicensePermissions : BaseEntity<DrivingLicensePermissions> {
#region Relations
public virtual DrivingLicense DrivingLicense { get; set; }
#endregion
public virtual DrivingLicenseCategory Category { get; set; }
}
映射:
class StudentMap : ClassMap<Student> {
public StudentMap() {
Id(x => x.Id);
Map(x => x.FirstName).Not.Nullable().Length(25);
Map(x => x.SecondName).Nullable().Length(25);
Map(x => x.LastName).Not.Nullable().Length(50);
Map(x => x.PESEL).Nullable().Length(11);
Map(x => x.BirthDate).Not.Nullable();
Map(x => x.City).Not.Nullable().Length(50);
Map(x => x.PostalCode).Nullable().Length(6);
Map(x => x.Street).Not.Nullable().Length(50);
Map(x => x.HouseNr).Not.Nullable().Length(10);
Map(x => x.ApartmentNr).Nullable().Length(10);
Map(x => x.PhoneNr).Nullable().Length(20);
Map(x => x.Email).Nullable().Length(100);
References(x => x.DrivingLicense).Nullable().Cascade.All();
References(x => x.User).Nullable().Cascade.All();
HasMany(x => x.Participants).Cascade.All();
}
}
class DrivingLicenseMap : ClassMap<DrivingLicense> {
public DrivingLicenseMap() {
Id(x => x.Id);
Map(x => x.IssueDate).Not.Nullable();
Map(x => x.DrivingLicenseNr).Not.Nullable().Length(20);
HasMany(x => x.DrivingLicensePermissions).Cascade.All();
References(x => x.Student).Not.Nullable();
}
}
class DrivingLicensePermissionsMap : ClassMap<DrivingLicensePermissions> {
public DrivingLicensePermissionsMap() {
Id(x => x.Id);
Map(x => x.Category).Not.Nullable();
References(x => x.DrivingLicense).Not.Nullable();
}
}
而我的问题是这样的例外:非空属性引用null或瞬时值Model.Entities.DrivingLicense 。学生,而我试图坚持学生对象这样
session.Save(student);
已分配DrivingLicense对象将其
的DrivingLicense财产。
我认为这是由于错误的映射 - 错误的级联或缺少反向引起的。我尝试了许多组合,但无法完成工作。 也是正确的,学生表有DrivingLicense_id和相反,DrivingLicense有Student_id列?!
投票总是很方便。同意你。如果他只设置'Student'的'id'而不是设置整个实体,它将工作。纠正我,如果我错了。 –
当然。我没有想到'学生'不是零但是暂时的情况。在那种情况下,存在级联问题。 –
看起来像[this](http://i.imgur.com/deJNCJr.png)。没有空。但是当我想添加一个没有DrivingLicense的Student时,Student的DrivingLicense属性为null,在持续期间没有错误。 所以有一个级联问题?但如何解决它? –