当前位置: 代码迷 >> 综合 >> Nhibernate: Foreign key must have same number of columns as the referenced primary key
  详细解决方案

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

热度:52   发布时间:2023-12-15 02:07:57.0

      近日在应用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

  相关解决方案