C# 组合键不尝试保留键值
好吧,我又在挠头了。我有一门课是这样的:C# 组合键不尝试保留键值,c#,postgresql,nhibernate,fluent-nhibernate,fluent-nhibernate-mapping,C#,Postgresql,Nhibernate,Fluent Nhibernate,Fluent Nhibernate Mapping,好吧,我又在挠头了。我有一门课是这样的: 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 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();
}
}
以及我的fluent映射文件:
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();
}
}
错误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))
…但我在尝试将记录合并到表中之前已指定了值。通过断点,我能够在提交会话事务之前验证对象是否确实具有值
我做错了什么?我需要指定键的值。我不记得NHibernate默认键生成器是什么,但请尝试添加它,告诉NH您将使用组件作为标识符来分配键。这种方法的例子不多,但是。以下是更新的代码:
// snipped >%
CompositeId()
.ComponentCompositeIdentifier<ReconciliationReportLineItemKey>
(rrli => rrli.Key)
.KeyProperty(k => k.Key.Id)
.KeyProperty(k => k.Key.Category);
// snipped >%
并将组件的属性添加到实体类:
public class ReconciliationReportLineItem
{
// snipped >%
public virtual ReconciliationReportLineItemKey Key { get; set; }
// snipped >%
}
不幸的是,这不是一个选项。在这种情况下,您仍然需要替换默认的生成器。您可以通过创建实现
IIdentifierGenerator
的类来创建自定义生成器。请查看中的代码以获取示例。在Fluent NHibernate中,您需要使用.GeneratedBy.Custom(…
将其映射到自定义生成器类。.GeneratedBy
不是CompositeIdentityPart
的可用属性;我想如果是,则是分配的()
方法将是可用的。你完全正确,我当时正在从头开始编写代码,并假设对单值键有效的方法对复合键也有效。我已经用另一种方法更新了答案,这种方法可能适用于你正在尝试的方法。这个问题非常相似:它是相似的。我将看看我是否能得到建议的内容在这种情况下,他示意要工作。
public class ReconciliationReportLineItemKey
{
public virtual int Id { get; set; }
public virtual int Category { get; set; }
}
public class ReconciliationReportLineItem
{
// snipped >%
public virtual ReconciliationReportLineItemKey Key { get; set; }
// snipped >%
}