gpt4 book ai didi

c# - 复合键不尝试保留键值

转载 作者:行者123 更新时间:2023-11-29 12:14:04 26 4
gpt4 key购买 nike

好吧,我又开始挠头了。我有这样的类(class):

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))

...但我在尝试将记录合并到表中之前已经指定了值。通过断点,我能够在提交 session 事务之前验证对象确实具有值。

我做错了什么?我需要指定键的值。

最佳答案

我不记得 NHibernate 默认 key 生成器是什么,但尝试添加它以告诉 NH 您将使用组件作为标识符来分配 key 。这种方法的例子不多,但是 this forum post is partial one .这是更新后的代码:

// snipped >%

CompositeId()
.ComponentCompositeIdentifier<ReconciliationReportLineItemKey>
(rrli => rrli.Key)
.KeyProperty(k => k.Key.Id)
.KeyProperty(k => k.Key.Category);

// snipped >%

您需要为键添加一个新类以进行分配:

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 >%
}

关于c# - 复合键不尝试保留键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13569953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com