流畅的NHibernate自动引用键

时间:2012-05-14 15:36:05

标签: nhibernate fluent-nhibernate

我最近通过NuGet从版本1.3.0.717和3.2.0.4000更新了我的项目到FNH 1.3.0.727和NH 3.3.0.4000。使用automap,更新前生成的XML如下所示:

  <class xmlns="urn:nhibernate-mapping-2.2" name="APP.Domain.Module, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Module">
    <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="ModuleId" />
      <generator class="guid.comb" />
    </id>
    <many-to-one class="APP.Domain.Product, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Product">
      <column name="ProductId" />
    </many-to-one>
    <joined-subclass name="APP.Domain.Audit.Trail, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key>
        <column name="TrailId" />
      </key>
      <bag cascade="all" inverse="true" lazy="true" name="Arguments">
        <key>
          <column name="TrailId" />
        </key>
        <one-to-many class="APP.Domain.Audit.Argument, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </bag>
      <property name="Action" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Action" />
      </property>
      <property name="Date" type="System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Date" />
      </property>
      <property name="Workflow" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Workflow" />
      </property>
      <property name="Identity" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="[Identity]" />
      </property>
    </joined-subclass>
  </class>

并在更新后:

  <class xmlns="urn:nhibernate-mapping-2.2" name="APP.Domain.Module, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Module`">
    <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="ModuleId" />
      <generator class="guid.comb" />
    </id>
    <many-to-one class="APP.Domain.Product, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Product">
      <column name="ProductId" />
    </many-to-one>
    <joined-subclass name="APP.Domain.Audit.Trail, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key>
        <column name="TrailId" />
        <column name="ModuleId" />
      </key>
      <bag cascade="all" inverse="true" lazy="true" name="Arguments">
        <key>
          <column name="TrailId" />
          <column name="TrailId" />
        </key>
        <one-to-many class="APP.Domain.Audit.Argument, APP.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </bag>
      <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Name" />
      </property>
      <property name="Date" type="System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Date" />
      </property>
      <property name="Workflow" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Workflow" />
      </property>
      <property name="Identity" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="[Identity]" />
      </property>
    </joined-subclass>
  </class>

我的模型中没有复合键,一切都是正确的,现在我开始异常"Foreign key (FK4B9386BFF4D27734:Argument [TrailId])) must have same number of columns as the referenced primary key (Trail [TrailId, ModuleId])"

我是否遗漏了新版本的内容?

2 个答案:

答案 0 :(得分:0)

问题在于约定和覆盖的组合。在每种情况下,前版本都会正确覆盖键列(外键,连接,hasmany,manytomany等),但现在正在添加它们。错误?

答案 1 :(得分:0)

使用IReferenceConvention而不是覆盖ForeignKeyconvention类中的GetKeyName解决了这个问题。