Search code examples
nhibernatenhibernate-mappingcascading-deletesdelete-record

Deleted object would be re-saved by cascade (remove deleted object from associations) in nhibernate


I keep getting this error when trying to delete a record from my table. Insert and Update work fine apart from delete.

Here is my set up:

Mappings:

 HasMany(x => x.Items).AsList().AsBag().LazyLoad().Cascade.AllDeleteOrphan();   

GetMethod:

  IRepositoryWithTypedId<BOD.Entities.Item, Guid> Rep = RepositoryFinder.For<BOD.Entities.Item, Guid>();
         BOD.Entities.Item tag = Rep.Get(new Guid("0A495241-082F-4314-8B79-000A524FC666"));

         Rep.Delete(tag); 

I have also tried using:

 Repository().DbContext.CommitTransaction();
 Repository().DbContext.CommitChanges();

These two still cause errors. Does anyone have any suggestions?


Solution

  • this is because tag is contained in some collection, you should remove it from that collection to actually delete it. Otherwise when you persist the collecting entity, the tag will be saved again.