简体   繁体   中英

Problem with lazy load soft deleted entities with Nhibernate

Hello everyone I have a problem I'll try to describe shortly In our application we use Nhibernate as ORM, and Fluent Nhibernate for mapping, and was implemented soft delete for entities that mean the entity doesn't remove physically for data base it is just set the property DeletedBy and DeletedDate with values. And the problem is that when the entities loaded with lazy load the soft deleted entities load too. After entities was loaded I need to filter where DeletedDate is null in each place in code where this entities is called, but this is not good.

How can resolve in general for all entities this problem with lazy load soft deleted?

Can you help me?

Thanks in advance!

With fluent Nhibernate map the problem is solving in the following way in class mapping for child collection map it is need to add the following statement

.Where("DeletedDate is null")

example

 HasMany(x => x.Registrations).Where("DeletedDate is null").KeyColumn("qualificationEnrolmentId").Inverse().Cascade.AllDeleteOrphan();

methods Where has override with LINQ expression but this not work in this case

Where(x=>x.DeletedDate == null)

this code doesn't work because x in lambda expression is recognized as parent entity and other override for method Where accept in parameter SQL query not HQL.

I haven't used Fluent-Hibernate so not sure if it would work for you though but for classic NHibernate:

In the HBM class mapping file put a where clause in :

<class  name="Class_Type" table="Entity_Table" where="DeletedBy is null" >
...
</class>

to return only those entities not logically deleted for every request

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