Sqlite with multiple NHibernate Session Factories
I have a problem in that I have switched my (working) multiple-database nhibernate solution to DI its session factory so that I can use SqlLite from within my integration test suite.
Whereas using a physical file where i can use multiple fi开发者_运维技巧le names, one for each db works (see code below), I am not sure how to do that with it in memory. Maybe its just a limitation inflicted by sqlite when in memory? What happens is that each subsequent call to InMemory() overwrites the existing one even though each factory gets its own key on the current thread.
public override Configuration BuildSessionFactoryFor(string databaseName, bool showSql, IsolationLevel level)
{
Configuration cfg = null;
var filename = @"c:\temp\{0}-test.db".Substitute(databaseName);
if (File.Exists(filename)) File.Delete(filename);
var sqlConfig =
SQLiteConfiguration.Standard.ShowSql().UsingFile(filename); //.InMemory()
var sessionFactory = Fluently.Configure()
.Database(sqlConfig)
.ProxyFactoryFactory(typeof (ProxyFactoryFactory))
.Mappings(m =>
m.AutoMappings.Add(
new AutoPersistenceModelGenerator().GenerateSchema(databaseName)
)
)
.ExposeConfiguration(c =>
{
new SchemaExport(c).Execute(false, true, false);
cfg = c;
})
.BuildSessionFactory();
NHibernateSession.AddConfiguration(FactoryKeyFor(databaseName), sessionFactory, cfg, null);
return cfg;
}
You probably close or dispose NHIbernate session. The scope of the in-memory database is not ISessionFactory. It is ISession, so every time you close it explicitly or in a 'using' statement the database will be gone.
From In-Memory Databases:
The database ceases to exist as soon as the database connection is closed. Every :memory: database is distinct from every other. So, opening two database connections each with the filename ":memory:" will create two independent in-memory databases.
精彩评论