NHibernate试图在删除操作上将ID更新为null

时间:2015-05-18 02:33:16

标签: c# xml visual-studio-2010 nhibernate

起初我有这些实体:Persona,Paciente,Alergia,Cita和Consulta。 当我尝试删除Persona时出现问题。我的数据库看起来像这样: enter image description here

我目前正在与Persona和Paciente合作。这些是他们的地图和课程:

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Odontologia General Admin" namespace="Odontologia_General_Admin.Persistence.Entidades" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Persona" table="persona" lazy="true" >
    <id name="Cipersona" column="ciPersona" />
    <bag name="Cliente" cascade="all-delete-orphan">
      <key column="ciCliente"/>
      <one-to-many class="Cliente" />
    </bag>
    <bag name="Paciente" cascade="all-delete-orphan">
      <key column="ciPaciente"/>
      <one-to-many class="Paciente" />
    </bag>
  </class>
</hibernate-mapping>

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Odontologia General Admin" namespace="Odontologia_General_Admin.Persistence.Entidades" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Paciente" table="paciente" lazy="true" >
    <id name="Cipaciente" column="ciPaciente" />
    <bag name="CitasPaciente" table="cita" lazy="true" inverse="true" cascade="all-delete-orphan">
      <key column="ciPaciente" />
      <one-to-many class="Cita"/>
    </bag>
    <bag name="ConsultasPaciente" table="consulta" lazy="true" inverse="true" cascade="all-delete-orphan">
      <key column="ciPaciente" />
      <one-to-many class="Consulta" />
    </bag>
    <bag name="AlergiasPaciente" table="alergia_paciente" lazy="true" cascade="all-delete-orphan" inverse="true" >
      <key column="ciPaciente"></key>
      <many-to-many column="idAlergia" class="Alergia" />
    </bag>
  </class>
</hibernate-mapping>

他们的实体:

public class Persona : Entidad {
    public Persona() { Paciente = new List<Paciente>();
    Cliente = new List<Cliente>();
    public virtual int Cipersona { get; set; }
    [Browsable(false)]
    public virtual IList<Cliente> Cliente { get; set; }
    [Browsable(false)]
    public virtual IList<Paciente> Paciente { get; set; }
}

    public class Paciente : Entidad
{
    public Paciente() { CitasPaciente = new List<Cita>();}
    public virtual int Cipaciente { get; set; }
    public virtual IList<Alergia> AlergiasPaciente { get; set; }
    public virtual IList<Cita> CitasPaciente{ get; set; }
    public virtual IList<Consulta> ConsultasPaciente { get; set; }
}

当我尝试删除一个角色并且级联尝试更新paciente并将其ID设置为null时出现问题

UPDATE paciente SET ciPaciente = null WHERE ciPaciente = ?p0

SQL抛出了异常,因为该列不能为空。

2 个答案:

答案 0 :(得分:3)

not-null="true"update="false"添加到Paciente集合映射:

<bag name="Paciente" cascade="all-delete-orphan">
  <key column="ciPaciente" not-null="true" update="false"/>
  <one-to-many class="Paciente" />
</bag>

此外,您可能希望将集合映射为反向。

答案 1 :(得分:0)

在行李映射中,您需要更新=&#34; false&#34;