Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLite database verification after nhibernate schema generation
    text
    copied!<p>What is the simplest most effective way to verify that your SQLite db is actually out there after using NHib's schema generation tool?</p> <p>Cheers,<br> Berryl</p> <p>EDIT</p> <p>I am hoping there is something tied to the ISession (like the connection property) that can be tested; sometimes when running a series of tests it seems like a good session (IsOpen &amp; IsConnected are true) but the db is not there (a query against it gets an error like 'no such table').</p> <p>EDIT - WHAT I AM DOING NOW</p> <p>Connection string &amp; other cfg properties</p> <pre><code>public static Configuration GetSQLiteConfig() { return new Configuration() .SetProperty(ENV.Dialect, typeof (SQLiteDialect).AssemblyQualifiedName) .SetProperty(ENV.ConnectionDriver, typeof (SQLite20Driver).AssemblyQualifiedName) .SetProperty(ENV.ConnectionString, "Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1") .SetProperty(ENV.ProxyFactoryFactoryClass, typeof (ProxyFactoryFactory).AssemblyQualifiedName) .SetProperty(ENV.ReleaseConnections, "on_close") .SetProperty(ENV.CurrentSessionContextClass, typeof (ThreadStaticSessionContext).AssemblyQualifiedName); } </code></pre> <p>How I test the db now, for lack of something 'better' (this tests the mappings)</p> <pre><code> public static void VerifyAllMappings(ISessionFactory sessionFactory, ISession session) { Check.RequireNotNull&lt;ISessionFactory&gt;(sessionFactory); Check.Require(session.IsOpen &amp;&amp; session.IsConnected); _verifyMappings(sessionFactory, session); } private static void _verifyMappings(ISessionFactory sessionFactory, ISession session) { try { foreach (var entry in sessionFactory.GetAllClassMetadata()) { session.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco)) .SetMaxResults(0).List(); } } catch (Exception ex) { Console.WriteLine(ex); throw; } } public static void VerifyAllMappings(ISessionFactory sessionFactory, ISession session) { Check.Require(!sessionFactory.IsClosed); Check.Require(session.IsOpen &amp;&amp; session.IsConnected); try { foreach (var entry in sessionFactory.GetAllClassMetadata()) { session.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco)) .SetMaxResults(0).List(); } } catch (Exception ex) { Debug.WriteLine(ex); throw; } } </code></pre> <p>I generate the schema in a session provider whenever a new session is opened:</p> <pre><code> public ISession Session { get { var session = (ISession)CallContext.GetData(_lookupSessionKey); try { if (session == null) { _log.Debug("Opening new Session for this context."); session = FactoryContext.Factory.OpenSession(); if(RunTypeBehaviorQualifier != RunType.Production) SchemaManager.GenerateNewDb(FactoryContext.Cfg, session.Connection); CallContext.SetData(_lookupSessionKey, session); } } catch (HibernateException ex) { throw new InfrastructureException(ex); } return session; } } </code></pre> <p>Now this is all probably way over engineered, but I need multiple database connections and I've been having trouble keeping it simpler &amp; working. It's also a lot of info for one question, but maybe someone else has actually got this all down to a science. The test below runs fine within it's own test fixture, but not in conjunction with other tests. </p> <pre><code> [Test] public void Schema_CanGenerateNewDbWithSchemaApplied() { DbMappingTestHelpers.VerifyAllMappings(_dbContext.FactoryContext.Factory, _dbContext.Session); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload