Nhibernate:外键必须与引用的主键具有相同的列数
我对这种情况感到困惑。分为以下两类:Nhibernate:外键必须与引用的主键具有相同的列数,nhibernate,orm,Nhibernate,Orm,我对这种情况感到困惑。分为以下两类: 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 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{
公共TxType()
{
付款=新列表();
}
公共字符串TxTypeId{get;set;}
公共字符串TxCode{get;set;}
公共IList付款{get;set;}
公共字符串说明{get;set;}}
公共类支付{
公共支付(){}
公共字符串Trn{get;set;}
公共TxType TxTypeId{get;set;}
公共字符串TxCode{get;set;}
公共系统.可为空的金额{get;set;}
公共系统.可为空的日期戳{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>
谢谢 您的id属性只能是一个。也就是说,如果表具有复合主键,则应该创建一个类来映射到这些列。请记住,Get/Load方法只对id使用一个参数,而不是多个参数
这不是问题的答案,但如果设置了
和
,则无法将任何付款添加到交易中