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

使用Ninject设置带有两个数据库的Nhibernate,nhibernate,ninject,Nhibernate,Ninject,我正在尝试设置我的应用程序,以便将审核信息保存在Sperate数据库中。我已经能够使用Ninject设置和配置NHibernate以使用一个数据库,但不能使用第二个数据库 这就是我尝试过的: public class NHibernateModule : NinjectModule { public override void Load() { Bind<ISessionFactory>() .ToMethod(c =&g

我正在尝试设置我的应用程序,以便将审核信息保存在Sperate数据库中。我已经能够使用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();
    }
}
公共类NHibernateModule:ninject模块
{   
公共覆盖无效负载()
{
绑定()
.ToMethod(c=>NHibernateHelper.CreateSessionFactory())
.InSingletonScope();
绑定()
.ToMethod(c=>NHibernateHelper.CreateLoggingSessionFactory())
.wheninjectedto()
.InSingletonScope();
绑定()
.ToMethod(c=>c.Kernel.Get().OpenSession());
}
}
公共静态类NHibernateHelper
{
公共静态ISessionFactory CreateSessionFactory()
{
var cfg=新配置();
返回cfg.Configure().SetProperty(“connection.connection\u string\u name”,“ApplicationServices”).BuildSessionFactory();
}
公共静态ISessionFactory CreateLoggingSessionFactory()
{
var cfg=新配置();
返回cfg.Configure().SetProperty(“connection.connection\u string\u name”,“AuditingServices”).BuildSessionFactory();
}
}
不幸的是,只有CreateSessionFactory()方法被调用,我无法获得到审计数据库的会话


任何帮助都将非常感激

条件必须在会话中

    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>();
Bind()
.ToMethod(c=>NHibernateHelper.CreateSessionFactory())
.已命名(“默认”)
.InSingletonScope();
绑定()
.ToMethod(c=>NHibernateHelper.CreateLoggingSessionFactory())
.命名(“日志记录”)
.InSingletonScope();
绑定()
.ToMethod(c=>c.Kernel.Get(“默认”).OpenSession();
绑定()
.ToMethod(c=>c.Kernel.Get(“日志”).OpenSession()
.当输入到()时;

这种情况也可能是错误的。BaseLoggingModel听起来好像派生了各种类。因此,如果类是从BaseLoggingModel派生的,而不是从InjectedTo派生的,则必须使用自己的条件检查,因为我无法使其工作。我有一个类似的问题,并在此处发布了一个问题:我有一个类似的问题,并在此处发布了一个问题: