Nhibernate: Foreign key must have same number of columns as the referenced primary key

      近日在应用NHibernate的表间关系时,主表有两个主键,子表有一个主键,但与主表关联。在调试时总时报“must have same number of columns as the referenced primary key ”的异常,结果发现应该这样解决。

类:

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; } }

映射文件:

<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 name="TxType" class="MyProject.Nhibernate.Repository.TxType" insert="false" update="false">
	  <column name="TxTypeId" sql-type="varchar" not-null="false" />
	  <column name="TxCode" sql-type="varchar" not-null="false" />
    </many-to-one>
    <property name="TxTypeId">
      <column name="TxTypeId" sql-type="varchar" not-null="false" />
    </property>
    <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>

转自http://stackoverflow.com/questions/11179371/nhibernate-foreign-key-must-have-same-number-of-columns-as-the-referenced-prima

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值