简体   繁体   中英

NHibernate: Laziness.NoProxy prevents soft delete

I have this for an NHibernate mapping:

    public AnswerSet_AnswerMap() {
        Table("DB.AnswerSet_Answer");
        Id(x => x.AnswerSet_AnswerId);
        References(x => x.Answer, "BaseAnswerID").LazyLoad(Laziness.NoProxy);
        References(x => x.AnswerSet, "AnswerSetID").Fetch.Join();
        Map(x => x.Format);
    }

It's for a junction table between an "AnswerSet" and an "Answer". The .LazyLoad(Laziness.NoProxy) on the answer reference is necessary in our application, but it prevents an AnswerSet_Answer object from being soft deleted in the database (it remains in the database unchanged). Does anyone know why this is happening?

Did you try adding a cascade? Cascade.DeleteAllOrphan ?

NHibernate Cascades: Entities has associations to other objects, this may be an association to a single item (many-to-one) or an association to a collection (one-to-many, many-to-any). At any rate, you are able to tell NHibernate to automatically traverse an entity's associations, and act according to the cascade option. For instance, adding an unsaved entity to a collection with save-update cascade will cause it to be saved along with its parent object, without any need for explicit instructions on our side. Here is what each cascade option means:

none - do not do any cascades, let the users handles them by themselves. save-update - when the object is saved/updated, check the associations and save/update any object that require it (including save/update the associations in many-to-many scenario). delete - when the object is deleted, delete all the objects in the association. delete-orphan - when the object is deleted, delete all the objects in the association. In addition to that, when an object is removed from the association and not associated with another object (orphaned), also delete it. all - when an object is save/update/delete, check the associations and save/update/delete all the objects found. all-delete-orphan - when an object is save/update/delete, check the associations and save/update/delete all the objects found. In additional to that, when an object is removed from the association and not associated with another object (orphaned), also delete it.

But cascade will delete your orpahnd record,not "soft-delete" it. for soft deletion look into this link : Soft Deletes In NHibernate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM