[英]Map table with composite key / foreign key to that table
I am trying to map a table that has a composite key and map another table that references this table. 我正在尝试映射具有复合键的表,并映射另一个引用该表的表。
Assume these tables: 假设这些表:
ITEMDELIVERY
with relevant columns: ITEMDELIVERY
及相关栏:
ITEMDELIVERY_ID
DELIVERY_DATE
ITEMDELIVERYDETAIL
with relevant columns: ITEMDELIVERYDETAIL
其中包含相关列:
ITEMDELIVERYDETAIL_ID
ITEMDELIVERY_ID
PARTITIONDATE
The columns ITEMDELIVERY.ITEMDELIVERY_ID
and ITEMDELIVERY.DELIVERY_DATE
together form the PK. ITEMDELIVERY.ITEMDELIVERY_ID
和ITEMDELIVERY.DELIVERY_DATE
列共同构成PK。
The columns ITEMDELIVERYDETAIL.ITEMDELIVERY_ID
and ITEMDELIVERYDETAIL.PARTITIONDATE
form the FK from ITEMDELIVERYDETAIL
to ITEMDELIVERY
. ITEMDELIVERYDETAIL.ITEMDELIVERY_ID
和ITEMDELIVERYDETAIL.PARTITIONDATE
列构成了从ITEMDELIVERYDETAIL
到ITEMDELIVERY
的FK。
How do I map this? 我该如何映射?
I tried the following: 我尝试了以下方法:
IAutoMappingOverride<ItemDeliveryDetail>
: IAutoMappingOverride<ItemDeliveryDetail>
:
mapping.References(x => x.ItemDelivery)
.Columns("ITEMDELIVERY_ID", "PARTITIONDATE");
IAutoMappingOverride<ItemDelivery>
: IAutoMappingOverride<ItemDelivery>
:
mapping.CompositeId().KeyProperty(x => x.Id, "ITEMDELIVERY_ID")
.KeyProperty(x => x.DeliveryDate, "DELIVERY_DATE");
But this doesn't work, it results in a System.InvalidCastException: Invalid cast from 'DateTime' to 'Double'.
但这是行不通的,它导致System.InvalidCastException: Invalid cast from 'DateTime' to 'Double'.
when calling SaveOrUpdate
on the session. 在会话上调用SaveOrUpdate
时。
UPDATE: 更新:
I just checked the generated SQL and it shows that NHibernate somehow switches the values: 我刚刚检查了生成的SQL,它显示NHibernate以某种方式切换了值:
INSERT INTO ITEMDELIVERYDETAIL
(ITEMDELIVERYDETAIL_ID, AMOUNT, PROCESSED_BY_REM, SINGLE_ITEM_PRICE,
ITEMDELIVERY_ID, PARTITIONDATE, SupplierInvoice_id)
VALUES (hibernate_sequence.nextval, :p0, :p1, :p2,
:p3, :p4, :p5)
returning ITEMDELIVERYDETAIL_ID into :nhIdOutParam;
:p0 = 20.12.2011 16:29:44 [Type: Double (0)],
:p1 = 6 [Type: DateTime (0)],
:p2 = 21.12.2011 16:29:44 [Type: Double (0)],
:p3 = 7 [Type: Int32 (0)],
:p4 = 0 [Type: DateTime (0)],
:p5 = 19.12.2011 16:29:44 [Type: Int32 (0)],
:nhIdOutParam = 27638398 [Type: Int32 (0)]
As you can see, the parameters are a complete mess... 如您所见,参数是一团糟。
They should be like this: 他们应该是这样的:
:p0 = 6 [Type: Double (0)],
:p1 = 21.12.2011 16:29:44 [Type: DateTime (0)],
:p2 = 7 [Type: Double (0)],
:p3 = 0 [Type: Int32 (0)],
:p4 = 19.12.2011 16:29:44 [Type: DateTime (0)],
:p5 = 27638398 [Type: Int32 (0)],
:nhIdOutParam = NULL [Type: Int32 (0)]
I solved the problem. 我解决了问题。 The reason for this strange behavior was that I had an explicit property PartitionDate
that also was mapped ( mapping.Map(x => x.PartitionDate).Column("PARTITIONDATE");
). 发生这种奇怪行为的原因是我有一个显式属性PartitionDate
,该属性也已映射( mapping.Map(x => x.PartitionDate).Column("PARTITIONDATE");
)。
This basically meant that the PARTITIONDATE
column was mapped twice. 这基本上意味着PARTITIONDATE
列被映射了两次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.