使用 Ninject 设置带有两个数据库的 Nhibernate

发布于 2024-12-04 10:42:19 字数 1293 浏览 1 评论 0原文

我正在尝试设置我的应用程序,以便将审核信息保存在单独的数据库中。我已经能够通过 Ninject 设置和配置 NHibernate 以使用一个数据库,但不能使用我的第二个数据库。

这是我尝试过的:

public class NHibernateModule : NinjectModule
{   
    public override void Load()
    {
        Bind<ISessionFactory>()
            .ToMethod(c => NHibernateHelper.CreateSessionFactory())
            .InSingletonScope();

        Bind<ISessionFactory>()
            .ToMethod(c => NHibernateHelper.CreateLoggingSessionFactory())
            .WhenInjectedInto<BaseLoggingModel>()
            .InSingletonScope();

        Bind<ISession>()
            .ToMethod(c => c.Kernel.Get<ISessionFactory>().OpenSession());


    }
}

public static class NHibernateHelper
{
    public static ISessionFactory CreateSessionFactory()
    {
        var cfg = new Configuration();
        return cfg.Configure().SetProperty("connection.connection_string_name", "ApplicationServices").BuildSessionFactory();
    }
    public static ISessionFactory CreateLoggingSessionFactory()
    {
        var cfg = new Configuration();
        return cfg.Configure().SetProperty("connection.connection_string_name", "AuditingServices").BuildSessionFactory();
    }
}

不幸的是,只有 CreateSessionFactory() 方法被调用,我无法获得审计数据库的会话。

任何帮助将不胜感激

I am trying to set up my application so that Audit information is saved on a spearate database. I have been able to set-up and configure NHibernate to use one database using Ninject, but not to my second database.

This is what I have tried:

public class NHibernateModule : NinjectModule
{   
    public override void Load()
    {
        Bind<ISessionFactory>()
            .ToMethod(c => NHibernateHelper.CreateSessionFactory())
            .InSingletonScope();

        Bind<ISessionFactory>()
            .ToMethod(c => NHibernateHelper.CreateLoggingSessionFactory())
            .WhenInjectedInto<BaseLoggingModel>()
            .InSingletonScope();

        Bind<ISession>()
            .ToMethod(c => c.Kernel.Get<ISessionFactory>().OpenSession());


    }
}

public static class NHibernateHelper
{
    public static ISessionFactory CreateSessionFactory()
    {
        var cfg = new Configuration();
        return cfg.Configure().SetProperty("connection.connection_string_name", "ApplicationServices").BuildSessionFactory();
    }
    public static ISessionFactory CreateLoggingSessionFactory()
    {
        var cfg = new Configuration();
        return cfg.Configure().SetProperty("connection.connection_string_name", "AuditingServices").BuildSessionFactory();
    }
}

Unfortunatley, only the CreateSessionFactory() method is ever called and I can't get a Session to my audit databse.

Any help would be greatly appreceiated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2024-12-11 10:42:19

条件必须在会话上。

    Bind<ISessionFactory>()
        .ToMethod(c => NHibernateHelper.CreateSessionFactory())
        .Named("Default")
        .InSingletonScope();

    Bind<ISessionFactory>()
        .ToMethod(c => NHibernateHelper.CreateLoggingSessionFactory())
        .Named("Logging")
        .InSingletonScope();

    Bind<ISession>()
        .ToMethod(c => c.Kernel.Get<ISessionFactory>("Default").OpenSession());
    Bind<ISession>()
        .ToMethod(c => c.Kernel.Get<ISessionFactory>("Logging").OpenSession())
        .WhenInjectedInto<BaseLoggingModel>();

条件也可能是错误的。 BaseLoggingModel 听起来好像您派生了各种类。因此,您必须使用自己的 When 条件检查该类是否派生自 BaseLoggingModel 而不是 WhenInjectedInto

The condition must be on the session

    Bind<ISessionFactory>()
        .ToMethod(c => NHibernateHelper.CreateSessionFactory())
        .Named("Default")
        .InSingletonScope();

    Bind<ISessionFactory>()
        .ToMethod(c => NHibernateHelper.CreateLoggingSessionFactory())
        .Named("Logging")
        .InSingletonScope();

    Bind<ISession>()
        .ToMethod(c => c.Kernel.Get<ISessionFactory>("Default").OpenSession());
    Bind<ISession>()
        .ToMethod(c => c.Kernel.Get<ISessionFactory>("Logging").OpenSession())
        .WhenInjectedInto<BaseLoggingModel>();

The condition might also be wrong. BaseLoggingModel sounds as if you derive various classes. So you have to use your own When condition checking if the class is derived from BaseLoggingModel instead of WhenInjectedInto

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文