Why is NHibernate a simple choice in the HQL database for SQLite not working?

I recently started using FluenNHibernate, and there was some strange problem when I tried to write unit test with SQLite.

I use SQLite in the database database for testing, for each testing method I clear the data existing in the database. In the example:

var u = new User() { Name = "Piotr" };
_session.Save(u);
_session.Clear();
var list = _session.CreateCriteria<User>().List();

This code works fine, but when I write in the following line:

var list2 = _session.CreateQuery("FROM User").List();

I get:

System.Data.SQLite.SQLiteException: SQLite error
no such table: users

NHibernate's SQL query is beautiful, so what could be the problem?

+3
source share
3 answers

, SQLite . NHibernate , .

IConnectionProvider, , .

:

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;
            }
        }
    }
}

connection.provider , .

InMemoryConnectionProvider.CreateConnection() , . , InMemoryConnectionProvider.DestroyConnection(), . IConnectionProvider.CloseConnection() no-op, NHibernate .

+2

, ? - .

0

I am having problems with SQLite using a database in memory. The database is destroyed when the connection / session is closed. Using a file option seems more efficient.

In addition, database entities must be created as part of a factory session. Try something like a factory setup session:

        // Create schema.
        new SchemaExport(config).Create(false, true);
0
source

Source: https://habr.com/ru/post/1714839/


All Articles