Paul
Paul

Reputation: 9541

NHibernate .Query returning stale data

This is my unit test (it's quite meaningless but proves a point

Config entity = new Config("key", "value");

Session.SaveOrUpdate(entity);

Config config = Session.Query<Config>().SingleOrDefault(c => c.Key == "key");
Assert.IsNotNull(config);

it fails...but I don't think it should (note, if I flush it, it doesn't fail, but that's not the behaviour I want)

If I replace the query line with this

Config config = Session.Get<Config>("key");

...it passes

At no point does it flush (I even set the FlushMode to never just to be sure). Why would one be successful, and the other not? This doesn't seem right - and I would very much like the linq one to be successful

Upvotes: 3

Views: 895

Answers (1)

Greg Fleming
Greg Fleming

Reputation: 502

This article:

http://ayende.com/Blog/archive/2009/04/30/nhibernate-ndash-the-difference-between-get-load-and-querying-by.aspx

Seems to suggest that a Query will go to the database, bypassing the session's cache, whereas Get will try the session first.

Upvotes: 5

Related Questions