Anyone have any idea why NHibernate would do this? Seems rediculuous to me and it's (obviously) crashing my app as it's trying to update primary key column. Here's what it's doing.
First, it inserts a record:
INSERT INTO WidgetConfigurationPositions
(WidgetId,
TargetId)
VALUES (256 /* @p0 */,
'row1-column2' /* @p1 */)
select SCOPE_IDENTITY()
The very next statement it issues is an UPDATE to that record!
UPDATE WidgetConfigurationPositions
SET WidgetConfigurationId = null,
TargetId = null
WHERE WidgetConfigurationId = 96 /* @p0 */
AND Id = 302 /* @p1 */
Dear lord why would it do this? Here is the relevant configuration for that entity:
<class name="Backplane.WidgetConfiguration, Backplane" table="WidgetConfigurations">
<id name="Id" column="Id">
<generator class="native" />
</id>
<property name="Name" column="ConfigurationName" />
<map name="Widgets" table="WidgetConfigurationPositions" cascade="al开发者_如何学运维l" lazy="false" fetch="select">
<key column="WidgetConfigurationId" />
<index column="TargetId" type="string" />
<one-to-many class="Backplane.WidgetPlacement"/>
</map>
</class>
<class name="Backplane.WidgetPlacement, Backplane" table="WidgetConfigurationPositions">
<id name="Id" column="Id">
<generator class="native" />
</id>
<many-to-one name="Widget" class="Backplane.Widget, Backplane" column="WidgetId" lazy="false" />
<property name="Target" column="TargetId" />
<map name="Options" table="PlacedWidgetOptions" cascade="all" lazy="false" fetch="select">
<key column="WidgetConfigurationPositionId"/>
<index column="OptionName" type="string" />
<element column="OptionValue" type="string" />
</map>
</class>
Did I miss something in my configuration?
This is perfectly documented in http://nhibernate.info/doc/nhibernate-reference/collections.html#collections-onetomany (read the last sentence)
I think you need an inverse=true on widgetplacement class.
精彩评论