You mentioned that you're using an in-memory SQLite database. NHibernate might be deciding to close the database connection in between the two statements, which would cause you to lose all of your tables.
One way of preventing this is to create a custom IConnectionProvider implementation that allows you to explicitly control when the connection is closed.
Here's what I used to handle this in a project:
public class InMemoryConnectionProvider : IConnectionProvider
{
private static readonly object syncObject = new object();
private static SQLiteConnection connection;
#region IConnectionProvider Members
public void Configure(IDictionary<string, string> settings)
{
}
public void CloseConnection(IDbConnection conn)
{
}
public IDbConnection GetConnection()
{
CreateConnection();
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
return connection;
}
public IDriver Driver
{
get { return new SQLite20Driver(); }
}
public void Dispose()
{
}
#endregion
public static void CreateConnection()
{
lock (syncObject)
{
if (connection == null)
{
var builder = new SQLiteConnectionStringBuilder
{
DataSource = ":memory:",
BinaryGUID = true,
DateTimeFormat = SQLiteDateFormats.ISO8601
};
connection = new SQLiteConnection(builder.ConnectionString);
connection.Open();
}
}
}
public static void DestroyConnection()
{
lock (syncObject)
{
if (connection != null)
{
connection.Dispose();
connection = null;
}
}
}
}
You'd also need to set the connection.provider
option in your config file to point to this class.
In your tests, you'd call the static InMemoryConnectionProvider.CreateConnection()
method either at the beginning of the test or in a setup method. When you're done, you'd call InMemoryConnectionProvider.DestroyConnection()
to close the connection. Since the implementation of IConnectionProvider.CloseConnection()
is a no-op, NHibernate won't be able to close the connection on its own.