1
我必須在我的數據庫中有兩個表:患者和地址。他們通過患者的地址和CorrespodencyAddress字段處於一對一關係。Fluent NHibernate:將NULL插入到FK列
這裏是患者代碼:
public class Patients
{
public virtual int Id { get; protected set; }
public virtual int? IndividualId { get; set; }
public virtual string First_Name { get; set; }
public virtual string Last_Name { get; set; }
public virtual string Pesel { get; set; }
public virtual string Gender { get; set; }
public virtual string Height { get; set; } //wzrost
public virtual string Comments { get; set; } //uwagi
public virtual Adresses Address { get; set; }
public virtual Adresses CorrespondencyAddress { get; set; }
public virtual StudyPayer DefaultPayer { get; set; }
public virtual DateTime? BirthDate { get; set; }
public virtual string PhoneNumber { get; set; }
public virtual DateTime? RegistrationDate { get; set; }
public virtual IList<Studies> Studies { get; set; }
public Patients()
{
Studies = new List<Studies>();
}
public virtual void AddAddress(Adresses adresses)
{
adresses.Patient = this;
Address = adresses;
}
public virtual void AddStudy(Studies studies)
{
studies.Patient = this;
Studies.Add(studies);
}
}
public class PatientsMap :ClassMap<Patients>
{
PatientsMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.IndividualId);
Map(x => x.First_Name);
Map(x => x.Last_Name);
Map(x => x.Pesel);
Map(x => x.Gender);
if (ConfigurationMap.DbType == DbType.PostgreSql)
{
Map(x => x.RegistrationDate).Nullable();
Map(x => x.BirthDate).Nullable();
}
if (ConfigurationMap.DbType == DbType.MsSql)
{
Map(x => x.RegistrationDate).CustomSqlType("datetime2").Nullable();
Map(x => x.BirthDate).CustomSqlType("datetime2").Nullable();
}
Map(x => x.PhoneNumber);
Map(x => x.Height);
Map(x => x.Comments);
References(x => x.Address).Cascade.All();
References(x => x.CorrespondencyAddress).Cascade.All();
References(x => x.DefaultPayer);
HasMany(x => x.Studies).Cascade.All().KeyColumn("patient_id");
}
}
下面是地址
public class Adresses
{
public virtual int Id { get; protected set; }
public virtual string Street { get; set; }
public virtual string HomeNumber { get; set; }
public virtual string PostalCode { get; set; }
public virtual string City { get; set; }
public virtual string Country { get; set; }
public virtual Patients Patient { get; set; }
public virtual void AddPatient(Patients patient)
{
patient.Address = this;
Patient = patient;
}
}
public class AdressesMap : ClassMap<Adresses>
{
AdressesMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.HomeNumber);
Map(x => x.Street);
Map(x => x.PostalCode);
Map(x => x.City);
Map(x => x.Country);
References(x => x.Patient).Cascade.All();
}
}
我們都知道有時CorrespodencyAddress相同地址碼的話,我想插入NULL到CorrespodencyAddress並且它將意味着它是相同的地址。
我該如何做到這一點?
如果我不喜歡下面,我得到的「可空對象必須有一個值」在運行時異常:
Patients p = new Patients();
p.CorrespondencyAddress = null;
我正在嘗試。不適合我。仍然是相同的錯誤... –
我期望你接收異常:*「非空屬性引用空值或瞬態值」* - 可以通過我的答案中提到的設置來解決:'.Nullable()'。所以,如果這沒有幫助,你能分享你的完整堆棧跟蹤嗎?通常情況下,異常幾乎都有答案....對於**可空對象必須有一個值**檢查答案在這裏http://stackoverflow.com/q/1896185/1679310 –
該問題不是由NH造成的,而是在日誌記錄功能是在事務提交之前進行的,並記錄了已提交的事務。我正在使用其他一些曾經使用過許多高級庫(Fluent NH)的代碼,但對OOP不太瞭解,代碼中存在垃圾,懶惰的同性戀等等。對不起。 –