【问题标题】:Why does hbm2ddl.SchemaExport not run here?为什么 hbm2ddl.SchemaExport 不在此处运行?
【发布时间】:2011-06-12 01:25:17
【问题描述】:

尝试在 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 “人”这个_] ---- System.Data.SQLite.SQLiteException : SQLite 错误

架构导出似乎正在工作 - 为什么没有创建表?

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

【问题讨论】:

    标签: .net nhibernate sqlite hbm2ddl


    【解决方案1】:

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

    一旦会话被释放,数据库基本上就被丢弃了。当您在构建架构和运行测试时使用不同的会话时,运行测试时架构不再存在。

    请参阅 Justin 中的 post 以获得非常清晰的解释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多