使用 Ninject 设置带有两个数据库的 Nhibernate
我正在尝试设置我的应用程序,以便将审核信息保存在单独的数据库中。我已经能够通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
条件必须在会话上。
条件也可能是错误的。 BaseLoggingModel 听起来好像您派生了各种类。因此,您必须使用自己的 When 条件检查该类是否派生自 BaseLoggingModel 而不是 WhenInjectedInto
The condition must be on the session
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