This是我的数据库项目的片段,this是Fluent NHibernate自动生成的数据库的片段
这些是我在上面数据库项目的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();
}
}
我的问题是这个例外: not-null属性引用了null或瞬态值Model.Entities.DrivingLicense.Student ,而我正试图坚持学生object 就像这样
session.Save(student);
已将DrivingLicense对象分配给它的DrivingLicense属性。
我认为它是由于错误的映射造成的 - 错误的级联或缺少反向。我尝试了很多组合,但却无法正常工作。 也是正确的,Student表有DrivingLicense_id,反之,DrivingLicense有Student_id列吗?!
答案 0 :(得分:0)
此问题的关键是DrivingLicense
通过Student
映射引用Not.Nullable()
。这意味着当NH尝试保留DrivingLicense
时,其Student
属性不得为null
。
调试代码并暂停Save(student)
调用。检查对象图,查看student
的{{1}}属性及其DrivingLicense
属性。我的猜测是Student
。
您需要查看地图的null
方面,或确保您的对象图表已正确连线。在坚持之前。
答案 1 :(得分:0)
要解决我的问题,我必须将参考引用更改为HasOne-to-Reference。
现在看起来像这样。
实体尚未更改
<强>映射强>:
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);
HasOne(x => x.DrivingLicense).PropertyRef(x => x.Student).Cascade.All();
References(x => x.User).Unique().Not.Nullable();
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).Unique().Not.Nullable();
}
}
class DrivingLicensePermissionsMap : ClassMap<DrivingLicensePermissions> {
public DrivingLicensePermissionsMap() {
Id(x => x.Id);
Map(x => x.Category).Not.Nullable();
References(x => x.DrivingLicense).Nullable();
}
}
感谢这一变化,循环引用已经消失。 see it here
学生表中没有DrivingLicense_id,现在我可以将学生实体与DrivingLicense一起保留,并且只是通过保存学生的权限,就像我以前那样。
session.Save(student);