【问题标题】:Fluent NHibernate: Insert NULL into FK columnFluent NHibernate:将 NULL 插入 FK 列
【发布时间】:2014-12-29 14:23:39
【问题描述】:

我的数据库中有两个表:Patients 和 Addresses。它们通过患者中的 Address 和 CorrespodencyAddress 字段是一对一的关系。

这里是患者的代码:

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 中,这意味着它是相同的地址。

我怎样才能做到这一点?

如果我像下面这样,我会在运行时得到“Nullable object must have a value”异常:

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

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate fluent-nhibernate-mapping


    【解决方案1】:

    你应该改变这个:

    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();
    

    检查所有可用的设置here (向下滚动到 Fluent NHibernate 的等效项

    也检查一下:

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

    还有这个映射:

    Map(x => x.IndividualId);
    

    应该是

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

    【讨论】:

    • 我希望您收到 Exception: "not-null property references a null or transient value" - 这可以通过我的回答中提到的设置来修复:.Nullable() .所以,如果这没有帮助,你能分享你的完整堆栈跟踪吗?通常异常几乎有答案....对于 Nullable object must have a value 在此处查看答案stackoverflow.com/q/1896185/1679310
    • 问题不是由 NH 引起的,而是在事务提交之前放置的日志记录函数中,并且正在记录该事务已提交。我正在使用其他人编写的代码,这些人曾经使用过许多高级库(Fluent NH),但对 OOP 了解不多,在代码中制造垃圾,是懒惰的同性恋等。抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2013-07-06
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多