问题描述
我试图映射具有复合键的表和地图另一个表引用此表
假设这些表:
-
ITEMDELIVERY
相关栏目:
-
ITEMDELIVERY_ID
-
DELIVERY_DATE
-
-
ITEMDELIVERYDETAIL
相关栏目:
-
ITEMDELIVERYDETAIL_ID
-
ITEMDELIVERY_ID
-
PARTITIONDATE
-
列 ITEMDELIVERY。 ITEMDELIVERY_ID
和 ITEMDELIVERY.DELIVERY_DATE
共同构成了PK。结果
的列 ITEMDELIVERYDETAIL.ITEMDELIVERY_ID
和 ITEMDELIVERYDETAIL.PARTITIONDATE
从 ITEMDELIVERYDETAIL
形成FK到 ITEMDELIVERY 。code>
如何映射这个
我试过如下:?
IAutoMappingOverride< ItemDeliveryDetail>
:
mapping.References(X => x.ItemDelivery)
.Columns(ITEMDELIVERY_ID,PARTITIONDATE);
IAutoMappingOverride< ItemDelivery>
:
mapping.CompositeId()KeyProperty(X => x.Id,ITEMDELIVERY_ID)。
.KeyProperty(X = > x.DeliveryDate,DELIVERY_DATE);
但是,这并不工作,它导致了 System.InvalidCastException:无效的转换,从日期时间到双师型。
打电话时 SaveOrUpdate
的会话。
更新:
我只是检查生成的SQL它表明NHibernate的莫名其妙切换值:
INSERT INTO ITEMDELIVERYDETAIL
(ITEMDELIVERYDETAIL_ID,数量,PROCESSED_BY_REM,SINGLE_ITEM_PRICE,
ITEMDELIVERY_ID,PARTITIONDATE,SupplierInvoice_id)
值(hibernate_sequence .nextval,:P0,:P1,:P2,
:P3,:P4,:P5)
返回ITEMDELIVERYDETAIL_ID到:nhIdOutParam;
:P0 = 20.12.2011 16时29分44秒[类型:双(0)],
:P1 = 6类型:DATETIME(0)],
: P2 = 21.12.2011 16点29分44秒[类型:双(0)],
:P3 = 7 [类型:的Int32(0)],
:P4 = 0类型:DATETIME(0 )],
:P5 = 19.12.2011 16时29分44秒[类型:的Int32(0)],
:nhIdOutParam = 27638398 [类型:的Int32(0)]
正如你所看到的,参数是一塌糊涂......结果
他们应该是这样的:
:P0 = 6类型:双人间(0)],
:P1 = 21.12.2011 16时29分: 44 [类型:日期时间(0)],
:P2 = 7 [类型:双(0)],
:P3 = 0类型:的Int32(0)],
:P4 = 19.12.2011 16点29分44秒[类型:日期时间(0)],
:P5 = 27638398 [类型:的Int32(0)],
:nhIdOutParam = NULL [类型:的Int32(0) ]
我解决了这个问题。这样做的原因奇怪的现象是,我有一个明确的财产 PartitionDate
也被映射( mapping.Map(X => x.PartitionDate) .Column(PARTITIONDATE);
)结果
这基本上意味着 PARTITIONDATE
列被映射两次
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_ID
DELIVERY_DATE
ITEMDELIVERYDETAIL
with relevant columns:ITEMDELIVERYDETAIL_ID
ITEMDELIVERY_ID
PARTITIONDATE
The columns ITEMDELIVERY.ITEMDELIVERY_ID
and ITEMDELIVERY.DELIVERY_DATE
together form the PK.
The columns ITEMDELIVERYDETAIL.ITEMDELIVERY_ID
and ITEMDELIVERYDETAIL.PARTITIONDATE
form the FK from ITEMDELIVERYDETAIL
to ITEMDELIVERY
.
How do I map this?
I tried the following:
IAutoMappingOverride<ItemDeliveryDetail>
:
mapping.References(x => x.ItemDelivery)
.Columns("ITEMDELIVERY_ID", "PARTITIONDATE");
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'.
when calling SaveOrUpdate
on the session.
UPDATE:
I just checked the generated SQL and it shows that NHibernate somehow switches the values:
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");
).
This basically meant that the PARTITIONDATE
column was mapped twice.
这篇关于复合键/外键,该表映射表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!