Question

Trying to play around with IInterceptor in NHibernate 2.1.2.4000 I've got the following bit of test code:

public class TestingNHibernateInterceptors
{
    [Fact]
    public void can_intercept_delete_for_audit_log()
    {
        FullyConfigureDb();
        Session(s => s.Linq<Person>().Any().ShouldBe(false));
    }
    ISessionFactory _sessions;
    void Session(Action<ISession> @do)
    {
        using (var s = _sessions.OpenSession())
        {
            @do(s);
            s.Flush();
        }
    }
    void FullyConfigureDb()
    {
        var cfg = CreateConfig();
        _sessions = cfg.BuildSessionFactory();
        BuildSchema(cfg);
    }
    Configuration CreateConfig()
    {
        return Fluently.Configure()
            .Database(new SQLiteConfiguration().InMemory())
            .Mappings(x => x.FluentMappings.Add<PersonMap>())
            .BuildConfiguration()
            .SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle")
            .SetProperty("show_sql", "true");
    }
    void BuildSchema(Configuration config)
    {
        var se = new NHibernate.Tool.hbm2ddl.SchemaExport(config);
        se.Execute(false, true, false, _sessions.OpenSession().Connection, null);
    }
    public class Person
    {
        public virtual Guid Id { get; private set; }
        public virtual string Name { get; set; }
    }
    public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
        }
    }
    public class AuditInterceptor : EmptyInterceptor, IInterceptor
    {
        public override void OnDelete(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
        {
            base.OnDelete(entity, id, state, propertyNames, types);
        }
    }
}

However. I keep getting the message:

failed: NHibernate.ADOException : could not execute query [ SELECT count(*) as y0_ FROM "Person" this_ ] [SQL: SELECT count(*) as y0_ FROM "Person" this_] ---- System.Data.SQLite.SQLiteException : SQLite error

Schema export seems to be working - why is the table not being created?

I'm guessing this has something to do with using an in memory sqllite but not sure what the issue is. Any ideas?

Was it helpful?

Solution

Been there, done that, got hurt too ;-)

Once the session is released, the database is basically dropped. As you use different sessions when building your schema and when running your tests, the schema doesn't doesn't exist any longer when the test is being run.

See this post from Justin for a very clear explaination.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top