为什么hbm2ddl.SchemaExport不在这里运行?

时间:2011-01-19 20:58:43

标签: .net nhibernate sqlite hbm2ddl

尝试在NHibernate 2.1.2.4000中使用IInterceptor我有以下测试代码:

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

然而。我不断收到消息:

  

失败:NHibernate.ADOException:   无法执行查询[SELECT   count(*)as y0_ FROM“Person”this_]   [SQL:SELECT count(*)as y0_ FROM   “人”this_]   ---- System.Data.SQLite.SQLiteException:   SQLite错误

模式导出似乎有效 - 为什么没有创建表?

我猜这与使用内存sqllite有关,但不确定问题是什么。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

去过那里,做到了,也受了伤; - )

会话发布后,数据库基本上被删除。在构建模式和运行测试时使用不同的会话时,运行测试时模式不再存在。

post查看此Justin,以获得非常明确的解释。