Hi, I know there are many threads about this around, but I've been trying everything I could see for a while now but still no luck.
I have an Item object, which contains a collection of itemdocument. Using nhibernate to link to the database, I can easily add to my collection of itemdocument, update items in it, etc, but whatever I do, I cannot delete.
I load the item from the same session I try to delete it from. I even added the session as a property of the item object, and use that property to save it, just in case.
If I use a Session.Delete() on an itemdocument in the collection, and then I re-save the item object (without removing the itemdocument from the collection), it actually creates a new object, which is to be expected. So that tells me my Save are working.
I hope this all makes sense, I'm getting a bit frustrated and might not be as clear as I want...
See my mappings for the item object:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="namespace.Business.BusinessEntity">
<class name="Item, namespace" table="[dbo].[Item]">
<id name="ItemId" column="ItemId" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="ItemCode" column="ItemCode" type="String" length="50" />
<property name="InternalDescription" column="InternalDescription" type="String" length="254" />
<property name="IsEnabled" column="IsEnabled" type="Boolean" />
<property name="Size" column="Size" type="String" length="50" />
<set name="Pictures" cascade="all-delete-orphan" inverse="true" >
<key column="ItemId" not-null="true"/>
<one-to-many class="ItemDocument" />
</set>
</class>
</hibernate-mapping>
Mapping for the Itemdocument object:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="namespace.Business.BusinessEntity">
<class name="ItemDocument, namespace" table="[dbo].[ItemDocument]">
<id name="ItemDocumentId" column="ItemDocumentId" type="Int32">
<generator class="identity" />
</id>
<property name="Order" column="[Order]" type="Int32" />
<many-to-one name="Item" class="Item" column="ItemId" not-null="true" />
<many-to-one name="Document" class="Document" column="DocumentId" cascade="save-update" not-null="true"/>
</class>
</hibernate-mapping>
Code for the delete:
//This does nothing
item.Pictures.Remove(item.Pictures.ElementAt(0));
session.SaveOrUpdate(item);
//This neither
session.Delete(item.Pictures.ElementAt(0));
item.Pictures.Remove(item.Pictures.ElementAt(0));
session.SaveOrUpdate(item);