我对这种情况感到困惑。有这两个类:
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),两者都是付款表中的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>
谢谢!
答案 0 :(得分:1)
您的id属性必须只有一个。也就是说,如果您的表具有复合主键,则应创建一个用于映射到这些列的类。请记住,Get / Load方法只为id提供一个参数,而不是很多。