0

我对这种情况感到困惑。有这两个类:

public class TxType {
    public TxType()
    {
        Payments = new List<Payment>();
    }
    public string TxTypeId { get; set; }
    public string TxCode { get; set; }
    public IList<Payment> Payments { get; set; }
    public string Description { get; set; } }

public class Payment {
    public Payment() { }
    public  string Trn { get; set; }
    public  TxType TxTypeId { get; set; }
    public  string TxCode { get; set; }        
    public  System.Nullable<decimal> Amount { get; set; }
    public  System.Nullable<System.DateTime> DateStamp { get; set; } }

TxType 表有两个 PK(TxTypeId,TxCode),在 Payment Table 中都是 FK。这些是我的映射:

  <class name="TxType" table="TxType" lazy="false" >
    <composite-id>
      <key-property name="TxTypeId" column="TxTypeId" />
      <key-property name="TxCode" column="TxCode" />
    </composite-id>
    <property name="Description">
      <column name="Description" sql-type="nvarchar" not-null="false" />
    </property>
    <bag name="Payments" inverse="true" cascade="none">
      <key>
        <column name="TxTypeId" />
        <column name="TxCode" />
      </key>      
      <one-to-many class="MyProject.Nhibernate.Repository.Payment" not-found="ignore" />
    </bag>
  </class>

  <class name="Payment" table="Payment" lazy="false" >
    <id name="Trn">
      <generator class="identity" />
    </id>
    <many-to-one insert="false" update="false" lazy="false" name="TxTypeId">
      <column name="TxTypeId" sql-type="varchar" not-null="false" />
    </many-to-one>
    <property name="TxTypeId">
      <column name="TxTypeId" sql-type="varchar" not-null="false" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="TxCode">
      <column name="TxCode" sql-type="varchar" not-null="false" />
    </many-to-one>
    <property name="TxCode">
      <column name="TxCode" sql-type="varchar" not-null="false" />
    </property>
    <property name="Amount">
      <column name="Amount" sql-type="decimal" not-null="false" />
    </property>
    <property name="DateStamp">
      <column name="DateStamp" sql-type="datetime" not-null="false" />
    </property>    
  </class>

而且我遇到了这个错误:外键(FKF4FA0208CDBA724F:Payment [TxTypeId]))必须与引用的主键(TxType [TxTypeId,TxCode])具有相同的列数。

谢谢!

好的,我通过在付款映射中添加这些行来弄清楚:

<many-to-one name="TxType" class="MyProject.Nhibernate.Repository.TxType" insert="false" update="false" lazy="false" >
  <column name="TxTypeId" sql-type="varchar" not-null="false" />
  <column name="TxCode" sql-type="varchar" not-null="false" />
</many-to-one>

谢谢!

4

1 回答 1

1

您的 id 属性必须只有一个。也就是说,如果你的表有一个复合主键,你应该创建一个类来映射到这些列。请记住,Get/Load 方法只接受一个参数作为 id,而不是很多。

于 2012-06-28T09:34:52.080 回答