好吧,我又在挠头了。我有这样的课:
public class ReconciliationReportLineItem
{
public virtual int ID { get; set; }
public virtual int Category { get; set; }
public virtual string FileName { get; set; }
public virtual decimal Amount { get; set; }
public virtual string BatchType { get; set; }
public virtual DateTime CreatedTimeStamp { get; set; }
public virtual string Currency { get; set; }
public virtual decimal LocalAmount { get; set; }
public virtual int NumberOfInvoices { get; set; }
public override bool Equals(object obj)
{
var t = obj as ReconciliationReportLineItem;
if (t == null) return false;
return
t.ID == this.ID
&& t.Category == this.Category;
}
public override int GetHashCode()
{
return string.Format("{0}{1}{2:yyyyMMddHHmmss}{3}{4}{5}{6}",
this.FileName, this.BatchType, this.CreatedTimeStamp,
this.NumberOfInvoices, this.LocalAmount, this.Amount,
this.Currency).GetHashCode();
}
}
和我的流利映射文件如下:
public class ReconciliationReportLineItemMapping : ClassMap<ReconciliationReportLineItem>
{
public ReconciliationReportLineItemMapping()
{
Table("ReconciliationReportLineItem");
CompositeId()
.KeyProperty(x => x.ID, "id")
.KeyProperty(x => x.Category, "category");
Map(x => x.FileName)
.Length(500)
.Nullable()
.Index("ixDatroseReconciliationReportLineItemFileName");
Map(x => x.Amount)
.Not.Nullable();
Map(x => x.BatchType)
.Not.Nullable();
Map(x => x.CreatedTimeStamp)
.Index("ixDatroseReconciliationReportLineItemCreatedTimeStamp")
.Not.Nullable();
Map(x => x.Currency)
.Not.Nullable();
Map(x => x.LocalAmount)
.Not.Nullable();
Map(x => x.NumberOfInvoices)
.Not.Nullable();
}
}
我填充对象,尝试提交它,我收到如下错误:
ERROR: 23505: duplicate key value violates unique constraint "reconciliationreportlineitem_pkey"
错误 sql 显示复合键的成员(已预设)设置为零:
INSERT INTO ReconciliationReportLineItem (FileName, Amount, BatchType, CreatedTimeStamp, Currency, LocalAmount, NumberOfInvoices, id, category)
VALUES (((NULL)::text), ((E'1065.47')::numeric), ((E'X200 batch created 20121027')::text), ((E'2012-10-27 08:39:00.000000')::timestamp), ((E'USD')::text), ((E'1065.47')::numeric), ((7)::int4), ((0)::int4), ((0)::int4))
...但是在尝试将记录合并到表中之前,我已经指定了值。使用断点,我能够在提交会话事务之前验证对象确实具有值。
我究竟做错了什么?我需要指定键的值。