Consider a typical NHiber开发者_运维问答nate context class.
public class SampleContext : NHibernateContext
{
    public SampleContext(ISession session)
        : base(session)
    { }
    public IQueryable<Person> People
    {
        get { return Session.Linq<Person>(); }
    }
    public Person GetPerson(int id)
    {
        get { return Session.Linq<Person>().SingleOrDefault(p => p.ID == id); }
    }
}
My question:
- How could I rewrite the GetPerson method to ignore the cache and retrieve data directly from the database?
There are a couple of ways to approach this problem:
- The Hibernate guys will tell you that you probably should be opening a different session in order to retrieve the latest data from the database. They would point out that the intention of the session is to be scoped to a relatively short-lived unit of work. 
- You could either put in a call to - Session.Refresh()inside your- GetPerson()method to always get the most current state from the database or you could expose that functionality through your own- Refresh()method.
- Alternatively, if you have a handle on the - Personobject itself, you could also try a- Session.Evict()to remove the- Personobject your session cache prior to loading it again.
In my experience, I've tried both #2 and #3 and have eventually always come around to refactoring to do #1.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论