簡體   English   中英

流利的NHibernate:在FK列中插入NULL

[英]Fluent NHibernate: Insert NULL into FK column

我在數據庫中必須有兩個表:Patients和Addresses。 它們通過患者中的“地址”和“對應地址”字段處於一對一關系。

這是患者的代碼:

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與Address相同,那么我想將NULL插入CorrespodencyAddress中,這意味着它是相同的地址。

我該如何實現?

如果我喜歡以下操作,則會在運行時收到“可為空的對象必須具有值”異常:

Patients p = new Patients();
p.CorrespondencyAddress = null;

您應該更改此:

References(x => x.Address).Cascade.All();
References(x => x.CorrespondencyAddress).Cascade.All();

對此(請參見.Nullable()設置)

References(x => x.Address).Cascade.All();
References(x => x.CorrespondencyAddress).Nullable().Cascade.All();

在此處檢查所有可用設置(向下滾動至Fluent NHibernate的等效設置

還要檢查一下:

public class Patients
{
    ...
    public virtual int? IndividualId { get; set; }

和這個映射:

Map(x => x.IndividualId);

應該是

Map(x => x.IndividualId).Nullable();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM