【问题标题】:Using fluent-nhibernate, is there convention to make the foreign-key column in one-to-many relation not null?使用fluent-nhibernate,是否有约定使一对多关系中的外键列不为空?
【发布时间】:2011-04-21 20:01:51
【问题描述】:

我正在使用 fluent-nhibernate 约定来映射我的实体:

 public class HasManyConvention : IHasManyConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IOneToManyCollectionInstance instance)
        {
            instance.Key.Column(instance.EntityType.Name + "ID");
            instance.Cascade.AllDeleteOrphan();
        }
    }

所以这个约定会生成以下休眠映射:

<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="ParentType, ParentTypeAssembly" table="ParentTable">
   <id name="ParentIDID" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
     <column name="ParentID" />
     <generator class="identity" />
   </id>

    <bag cascade="all-delete-orphan" name="Childs" mutable="true">
      <key>
        <column name="ParentID" />
      </key>
      <one-to-many class="ChildType, ChildTypeAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </bag>
  </class>
</hibernate-mapping>

如何修改约定以使子表中的 ParentID 不为空?

所以它看起来像这样:

  <key>
    <column name="ParentID" not-null="true" />
  </key>

【问题讨论】:

    标签: fluent-nhibernate null mapping conventions one-to-many


    【解决方案1】:

    在约定中似乎没有任何方法可以做到这一点。查看 tests 以实现 HasManyConvention 和 IOneToManyCollectionInstance 接口上的方法,我看不到任何设置非空值的方法。

    不过,您可能想看看这个question

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      相关资源
      最近更新 更多