【问题标题】:Why Aren't Deletes Cascading With This NHibernate Configuration File?为什么不使用此 NHibernate 配置文件进行级联删除?
【发布时间】:2011-09-28 03:10:12
【问题描述】:

我有以下 NHibernate 文件,但是由于两个答案表中的任何一个都存在外键约束,我不能删除问题。所需的行为是在删除相应的问题并在 Answers 元素上设置级联设置后删除 Answers。

下面是配置文件,大家看看是什么问题

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="Test.Domain" assembly="Test.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Question" abstract="true">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <discriminator />
    <property name="Text" length="500" />
    <property name="Note" length="2000" />
    <property name="DateRun" />
    <many-to-one name="ChoiceType" column="ChoiceTypeId" />
    <bag name="Answers" inverse="true" cascade="all,delete-orphan">
      <key column="QuestionId" on-delete="cascade" />
      <one-to-many class="Answer" />
    </bag>
  </class>
  <class name="Answer" abstract="true">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <many-to-one name="Question" column="QuestionId" />
    <many-to-one name="Group" column="GroupId" />
    <property name="Comment" />
  </class>
  <class name="Choice">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Text" length="500" />
  </class>
  <class name="ChoiceType">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Name" />
    <property name="Type" />
    <list name="Choices" cascade="all,delete-orphan">
      <key column="ChoiceTypeId" />
      <list-index column="ChoicesPos" />
      <one-to-many class="Choice" />
    </list>
  </class>
  <class name="Division">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Name" />
  </class>
  <union-subclass name="FreeTextAnswer" extends="Answer">
    <property name="Text" length="500" />
  </union-subclass>
  <union-subclass name="MultipleChoiceAnswer" extends="Answer">
    <many-to-one name="Choice" column="ChoiceId" />
  </union-subclass>
  <subclass name="MultipleChoiceQuestion" extends="Question" />
  <subclass name="FreeTextQuestion" extends="Question" />
</hibernate-mapping>

【问题讨论】:

    标签: nhibernate nhibernate-mapping


    【解决方案1】:

    不是吗 cascade="all-delete-orphan"

    ...带有连字符,而不是两个枚举值?

    【讨论】:

      【解决方案2】:

      您的答案集合映射应如下所示:

      <bag name="Answers" inverse="true" cascade="all-delete-orphan">
        <key column="QuestionId"/>
        <one-to-many class="Answer" />
      </bag>
      

      我删除了on-delete="cascade"。当您想从问题中删除一个答案时,您将不得不“追逐指针”。将对问题的引用设置为 NULL,并将其从答案集合中删除:

      answer.Question = null;
      

      【讨论】:

        猜你喜欢
        • 2015-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 2015-12-23
        • 1970-01-01
        • 1970-01-01
        • 2010-12-13
        相关资源
        最近更新 更多