代码之家  ›  专栏  ›  技术社区  ›  berko

我可以在初始配置(运行时)后关闭NHibernate ShowSQL吗

  •  6
  • berko  · 技术社区  · 17 年前

    我的NHibernate配置设置为显示所有交互的sql。因此,我的一些大型集成测试执行得很差(尤其是在生成测试报告时)。

    有没有一种方法可以在运行时关闭ShowSql,然后通过编程将其重新打开。

    1 回复  |  直到 17 年前
        1
  •  3
  •   Erik Öjebo    17 年前

    可以在运行时对配置对象使用SetProperties(),然后根据该配置创建SessionFactory。SetProperties将字典作为参数。然后,新的SessionFactory将使用新的配置设置。

            IDictionary<string, string> props = new Dictionary<string, string>();
            props["show_sql"] = "true";
    
            Configuration config = new NHibernate.Cfg.Configuration();
            config.SetProperties(props);
            config.Configure();
            config.AddAssembly(typeof(User).Assembly);
    
            ISessionFactory factory = config.BuildSessionFactory();
    

    有关更多信息,请查看文档的这一部分: ISessionFactory Configuration

    希望有帮助。

    /埃里克