部分类中属性的级联值

时间:2018-11-05 15:07:42

标签: c# nhibernate nhibernate-cascade

我有一个映射和POCO如下。问题是保存该对象时tbFNamesFeature未被更新(而tblFeature被保存) 我为cascade尝试了不同的值,但没有任何效果,所以我不得不说有些东西我没有得到。 (请参阅xml中的???)。
我在做什么错了?

tbFNamesFeature有2列:

  

FNamesId(PK,整数,不为null)
  FeatureId(PK,int,not null)

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Feature, Infrastructure.Interface"
         table="tblFeature">
    <id name="Id" type="Int32" unsaved-value="0">
      <column name="FeatureId" sql-type="int" not-null="true" unique="true" index="PK_tblFeature" />
      <generator class="native" />
    </id>
    <property name="Description" type="String">
      <column name="Description" length="100" sql-type="varchar" not-null="false" />
    </property>
    <bag name="FNames" table="tbFNamesFeature" inverse="true" lazy="false" cascade="???">
      <key>
        <column name="FeatureId" sql-type="int" not-null="true" />
      </key>
      <many-to-many class="FName, Infrastructure.Interface">
        <column name="FNamesId" sql-type="int" not-null="true" />
      </many-to-many>
    </bag>
  </class>
</hibernate-mapping>

Feature.cs

public partial class Feature : System.IComparable
{
        protected int id;
        protected string description;

        public virtual int Id
        {
            get { return this.id; }
            set { this.id = value; }
        }

        public virtual string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }
}

Feature.part.cs

public partial class Feature : System.IComparable
{
        private System.Collections.Generic.IList<FName> fnames;

        public virtual System.Collections.Generic.IList<FName> FNames
        {
            get
            {
                if (this.fnames == null)
                {
                    this.fnames = new System.Collections.Generic.List<FName>();
                }
                return this.fnames;
            }
            set {
                this.fnames = value;
            }
        }
 }

1 个答案:

答案 0 :(得分:1)

经过编辑以反映讨论:由于tblFNamesFeature是一个多对多表,因此需要使用inverse =“ false”设置映射,以表明该关系的另一端不负责保存收藏。

还需要设置

cascade =“ all”(包括保存,更新和删除)。