Updating Nhibernate child property is acting weird!
This is my application setup: Web client <-> WCF 开发者_如何学PythonService <-> Domain Repositories <-> NHibernate <-> Database
All projects share the same domain entities, through "shared DLLs".
One of the entities (Attribute) got a collection of child entities (Option). The mapping look like this:
// Attribute mapping
<class name="Attribute" lazy="false" table="Attributes">
<id name="Id" column="AttributeId" type="System.Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Name" type="System.String" length="100" not-null="true" />
<bag name="Options" generic="true" lazy="false" cascade="all-delete-orphan">
<key column="AttributeId" foreign-key="AttributeId" />
<one-to-many class="AttributeOption" not-found="ignore" />
</bag>
</class>
// Attribute option mapping, which is a child of Attribute
<class name="AttributeOption" lazy="false" table="AttributeOptions">
<id name="Id" column="OptionId" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="AttributeId" type="System.Int32" not-null="true" />
<property name="Name" type="System.String" length="100" not-null="true" insert="true" update="true" />
<property name="Order" column="OrderVal" type="System.Int32" not-null="true" insert="true" update="true" />
</class>
Everything seems to work fine, except when trying to update one of the OPTION properties.
To be more precise; The value IS updated every time I'm in Visual Studio debug-mode, and manually click "Expand result" on the IEnumerable returned from the following line of code:
var attrib = tempAttribute.Options.Where(e => e.Id == parsedId).Select(e => e.Name = model.EditRequest.Name);
However, if I don't do a manual "expand result" (like I've done on the image below), it doesn't seem to update the database either.
a busy cat http://i54.tinypic.com/311ungn.gif
How is this even possible? It doesn't make much sense to me..
Any help or theories is very appreciated! :-)
Thanks a lot!
The reason is attrib does not actually contain the list of items. It contains an iterator. So the Select part does not get executed automatically unless you enumerate attrib. Which is what you're doing when you expand the results. I would suggest manually doing the setting so that it's a lot more obvious what is happening or consider using a tool like Automapper or ValueInjecter.
精彩评论