Fluent NHibernate gotchas when testing with an in memory database.
May 29th, 2009 . by Daniel HölblingWhat I love most about programmatic configuration is that it’s close to the test.
While we were carrying dozens of XML files around for testing before, now with DSL based configuration everywhere the configuration is usually pretty near to the test fixture, instead of residing in some arbitrary XML that only insiders can associate with the test.
The standard sample for using SqlLite and Fluent NHibernate usually looks like this:
return
Fluently
.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile("mydb.db3").ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionFactory>())
.ExposeConfiguration(SaveSchema)
.BuildSessionFactory();
Where SaveSchema is a method that does a database rebuild.
Now, Fluent Nhibernate has in-memory databases built into the API. Just remove the UsingFile directive and you replace it with:
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
Charming isn’t it? Now the only problem is that you won’t be able to do anything with that DB since there is no schema present.
The in-memory database exists per session, so once you close the ISession the db is gone. Since the schema export from most samples operates in it’s own ISession the subsequent queries will still hit a blank database, and you’ll get an error stating there is no such table.
So my SessionFactory implementation had to change, since I needed to keep the configuration around for doing the schema export:
public class SessionFactory
{
public static ISessionFactory CreateSessionFactory()
{
return
Fluently
.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionFactory>())
.ExposeConfiguration((c) => SavedConfig = c)
.BuildSessionFactory();
}
private static Configuration SavedConfig;
public static void BuildSchema(ISession session)
{
var export = new SchemaExport(SavedConfig);
export.Execute(true, true, false, false, session.Connection, null);
}
}
And my tests then use a another factory method to construct the ISession object:
public static ISession CreateSession()
{
var factory = SessionFactory.CreateSessionFactory();
var session = factory.OpenSession();
SessionFactory.BuildSchema(session);
return session;
}
Hope this helps, quite an annoying problem and imo a far from perfect solution. Someone on the FNH mailing list suggested looking at the OneToManyIntegrationTester class but I couldn’t really extract any terribly useful information from there.



Thank you so much for this post, you have finally ended my pain trying to get this to work! What I hadn’t worked out was that you should save the configuration to use later in the schema export. Seems so simple now…
[...] the schema into the in-memory database. For the latter I’m grateful to Daniel Hölbling for his post describing how to do the latter using Fluent. Here’s the finished [...]
[...] the title sounds familiar to you, it’s intentional. After having to deal with this in pure NHibernate it came around to also bite me with [...]
[...] Ayende’s blog post, Krzysztof Kozmic’s recent post on Devlio.us, Daniel Hoebling’s blog post, Ayende’s NHProfiler [...]
Thanks for the post.
I was wondering whether it would be helpful to create the session factory once per test fixture rather than once per session, which will be created once per test within a fixture.
Actually, I am a strong believer in tests not interfering with each other.
By having all of my tests in one test-class share the same database instance, I am giving it shared state.
With a shared state among my tests I might run into issues of tests depending on order of execution to pass, thus making them pretty much worthless.
The alternative is to do a manual cleanup after each fixture to return the shared session back to a known state so it can be used for the next test. And imo that's not really worth the effort
greetings Daniel
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
You saved many hairs in my head from the threat of SqlLite induced stress. nnGreat Article
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
Glad it helped..
You saved many hairs in my head from the threat of SqlLite induced stress.
Great Article
Glad it helped..