[英]Fluent NHibernate: Insert NULL into FK column
I have to two tables in my database: Patients and Addresses. 我在数据库中必须有两个表:Patients和Addresses。 They are in one-to-one relation throught Address and CorrespodencyAddress fields in Patients.
它们通过患者中的“地址”和“对应地址”字段处于一对一关系。
Here is the code for Patients: 这是患者的代码:
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");
}
}
Here is code for Addresses 这是地址的代码
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();
}
}
As we all know sometimes CorrespodencyAddress is the same as Address, then I'd like to insert NULL into CorrespodencyAddress and it will be meaning it is the same address. 众所周知,有时候CorrespodencyAddress与Address相同,那么我想将NULL插入CorrespodencyAddress中,这意味着它是相同的地址。
How can I achieve that? 我该如何实现?
If I do like below that I get "Nullable object must have a value" exception in runtime: 如果我喜欢以下操作,则会在运行时收到“可为空的对象必须具有值”异常:
Patients p = new Patients();
p.CorrespondencyAddress = null;
You should change this: 您应该更改此:
References(x => x.Address).Cascade.All();
References(x => x.CorrespondencyAddress).Cascade.All();
Into this (see .Nullable()
setting) 对此(请参见
.Nullable()
设置)
References(x => x.Address).Cascade.All();
References(x => x.CorrespondencyAddress).Nullable().Cascade.All();
Check all the available settings here (scroll down to Fluent NHibernate's equivalent ) 在此处检查所有可用设置(向下滚动至Fluent NHibernate的等效设置 )
Also check this: 还要检查一下:
public class Patients
{
...
public virtual int? IndividualId { get; set; }
and this mapping: 和这个映射:
Map(x => x.IndividualId);
which should be 应该是
Map(x => x.IndividualId).Nullable();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.