Selecting from Sqlite db using NHIbernate returns nothing when data is actually present
I have a simple app that uses a sqlite db in my data layer, nothing major. I have populated a few records for UI testing, but I can't pull the data back at all.
The thing is that my my unit tests are able to CRUD without fail, but I'm concerned that something isn't right when I simply want to select all records from a table like to populate a gridview.
I'm using NHibernate for the first time in a loooooong time so I can't tell if this s setup issue or something else.
Base selection method:
protected IList<T> SelectMultipleInternalALL()
{
// create session and select.
using (ISession session = NBHelper.OpenSession())
return session.CreateCriteria<T>().List<T>();
}
Helper class:
public class NBHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(Assembly.GetCallingAssembly());
new SchemaExport(configuration).Execute(false, true, false);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
Table config:
<class name="UserData" table="User">
<id name="Id" type="System.Int32" column="Id" >
<generator class="native"/>
</id>
<property name="Email" />
<property name="FirstName" />
<property name="LastName" />
<property name="Notes" />
<property name="Favorite" type="boolean" />
<bag name="Addresses" inverse="true" cascade="all" generic="true">
<key column="UserId" />
<one-to-many class="AddressData"/>
</bag>
<bag name="Tags" inverse="true" cascade="all" generic="true">
<key column="WhatId" />
<one-to-many class="TagData"/>
</bag>
NHibernate config:
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLiteDriver</property>
<property name="connection.connection_string">
Data Source=C:\Documents and Settings\bryang\Desktop\AddressBook\AddressBook.DAL\addressbook.s3db
</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="show_sql">true</property>
The database location is totally valid, but I get no records. I think the issue might be in the CreateCriteria() call but my unit tests are 100% able to insert and read back the data. I'm missing something and I can't figure out what.
EDIT I was also running into a lazy loading exception from my one-to-many relationships. I made the collection columns lazy="false" which cleared up the error. A side-effect is that now I return data everywhere, but it's unit test data from somewhere and when I open the sqlite db I still only see where my small set of data was created. Where is this data being held on to? I'm not managing my ISession manually, and I'm even flushing after simple things like a select. Where on earth is this data coming from?
At this point I'll take some data over none, but it makes no sense to me开发者_JAVA技巧 if the db file I'm looking at has nothing in it. Thanks,
Bryan
First of all, change the Driver to the newer version of the driver, the SQLiteDriver is for the old finstar driver, and it has some quirks with the newer version of SQL Lite. Also, your database path needs to be changed, you can't reference a path with spaces without putting "'s around it. If you put your database in the same directory as your executable, you can just use the db name. This configuration works great for me :
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">Data Source=core.db3;Version=3</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="show_sql">true</property>
精彩评论