强制NHibernate获取已经处于持久状态/1级缓存的实体 [英] Force NHibernate to get entity that is already in persisted state / 1st level cache

查看:92
本文介绍了强制NHibernate获取已经处于持久状态/1级缓存的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务对象,负责一些业务逻辑验证.在将更新发布到存储库之前,它的工作是检查它所处理的实体是否符合某些业务规则.

必须检查的规则之一是,与数据库中的实体相比,实体的 Status 属性是否没有更改.因为我使用的存储库共享相同的 ISession ,所以当我尝试从数据库中获取实体时,为了获得比较对象:

if (fromDbEntity.Status != entity.Status) throw new Exception("Cannot change status...");

我将始终从第一级缓存中获取 fromDbEntity -因此,我在同一个对象上工作.

有没有办法强迫NHibernate/Repository从数据库中获取实体,即使它已经处于会话范围之内?

解决方案

在加载fromDbEntity

之前从会话中逐出entity

session.Evict(entity);

您可以查看官方文档以了解更多详细信息:管理缓存

与此有关的缺点是,由于现在该对象不在会话中,因此您将需要手动为entity调用SaveOrUpdate.

I have a service object that is responsible for some business logic validation. What it does, before issing Update to repository, is checking whether entity that it works on complies to some business rules.

One of that rules that is must check is if Status property of the entity didn't change when compared to entity that is in database. Because I use repositories that share the same ISession, when I try to get entity from database, in order to get an object for comparsion:

if (fromDbEntity.Status != entity.Status) throw new Exception("Cannot change status...");

I'll always get fromDbEntity that is in 1st level cache - so I work on the same object.

Is there a way to force NHibernate/Repository to get entity from database even though it's already in the scope of the session?

解决方案

Evict entity from session before loading fromDbEntity

session.Evict(entity);

You can check the official documentation for more details: Managing the caches

The con about this is that you will need to manually call SaveOrUpdate for entity since now the object is out of the session.

这篇关于强制NHibernate获取已经处于持久状态/1级缓存的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆