开发者

Problem with lazy load soft deleted entities with Nhibernate

开发者 https://www.devze.com 2023-03-24 01:33 出处:网络
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,

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 loa开发者_运维问答d 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

0

精彩评论

暂无评论...
验证码 换一张
取 消