【问题标题】:How to stop Fluently.Configure connecting automatically如何停止 Fluently。配置自动连接
【发布时间】:2013-01-21 10:52:59
【问题描述】:

如何防止 Fluently.Configure 自动连接到数据库?

我们的设置详述如下。

我们有以下静态方法来创建会话工厂:

public static ISessionFactory CreateSessionFactory()
        {
          return Fluently.Configure()
              .Database(
                 IfxOdbcConfiguration
                .Informix
                .ConnectionString("DSN=ODBCCONNECTION")
                .Driver<CompanyCore.Nhibernate.Core.ProfiledODBCClientDriver>()
                .Dialect<InformixDialect1000>()
                .ShowSql() 
              )
              .Mappings(
              m => m.FluentMappings.AddFromAssemblyOf<Task>()
              )
              .BuildSessionFactory();
        }

它与我们的 IOC 设置一起用于创建 SessionFactory,但是,由于我们的数据库团队的要求,每个用户都必须以他们的用户身份打开他/她自己的连接(使用非个人化) - 以允许进程识别,查看谁在 Informix 服务器上运行什么。

创建我们使用的单个连接:

public static ISession GetMySession(ISessionFactory factory, string user)
{
    CompanyCore.Nhibernate.Core.ProfiledODBCClientDriver drv = new Enact.Nhibernate.Core.ProfiledODBCClientDriver();

    drv.Configure(new Dictionary<string, string>());

    string conn = "DSN=ODBCCONNECTION";

    IDbConnection db = drv.CreateConnection();

    db.ConnectionString = conn;
    db.Open();

    return factory.OpenSession(db);
}

当我们的 UOW 调用 dispose 时,这些连接被关闭。

我们在APP启动时调用CreateSessionFactory(),然后通过IOC传递这个工厂,但是此时没有非个性化用户,因此在第一次加载时导致应用错误。

我们曾考虑过使用标准用户来访问诸如首次加载之类的事情,但肯定有更好的方法吗?

【问题讨论】:

    标签: nhibernate fluent-nhibernate inversion-of-control unity-container informix


    【解决方案1】:

    您需要禁用自动关键字引用: configuration.SetProperty(Environment.Hbm2ddlKeyWords, "none")

    有关详细信息,请参阅“Prevent NHibernate to connect to the database on configuration.BuildSessionFactory()”这篇文章: https://groups.google.com/forum/?fromgroups=#!topic/nhusers/F8IxCgYN038

    【讨论】:

    • 完美 - 额外链接的道具 +1
    猜你喜欢
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2020-08-31
    • 2015-04-14
    • 2011-01-05
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多