I have tried to set up SQLite as an inmemory database so that I can perform unit tests on my data access layer
(
http://ayende.com/Blog/archive/2006/10/ ... ecord.aspx)
The problem occures when I call session.Flush(). It seems my database connection is reset. This seems to reset the schema that I have created and I get an error message telling me "SQLite error no such table: tblPerson"
Do you have any solutions for this problem ?
How do I insert multiple items to the inmemory database, and then query the database ?
Code:
using (ISession session = factory.OpenSession())
{
IDbConnection connection = session.Connection;
new SchemaExport(cfg).Execute(false, true, false, true, connection, null);
List<Person> list = new List<Person>{
new Person{Name="John Rambo", Id=0},
new Person{Name="Sylvester Stallone", Id=1}
};
foreach (Person p in list)
session.Save(p);
session.Flush();
if (session.Connection != connection)
{
Console.WriteLine("Connection changed");
}
else
{
//I never come this far
foreach (Person p in list)
session.Evict(p);
ICriteria criteria = session.CreateCriteria(typeof(Person));
criteria.Add(Expression.Eq("Name", "John Rambo"));
var result = criteria.List();
foreach (var o in result)
Console.WriteLine((o as Person).Name);
}
Console.Read();
}
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="SqliteNHibernate"
namespace="SqliteNHibernate">
<class name="Person" table="tblPerson">
<id name="Id" column="Id">
<generator class="assigned"/>
</id>
<property name="Name" column="Name"/>
</class>
</hibernate-mapping>
Code:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">Data Source=:memory:;Version=3;New=True;</property>
</session-factory>
</hibernate-configuration>