试图将NHibernate SessionFactory设置为单例,将Session设置为作用域,所有这些都由.NET Core依赖项注入处理。我在Startup.cs中配置了这些代码:
services.AddSingleton<NHibernate.ISessionFactory>(factory =>
{
return Fluently
.Configure()
.Database(() =>
{
return FluentNHibernate.Cfg.Db.MsSqlConfiguration
.MsSql2012
.ShowSql()
.ConnectionString(ConnectionString);
})
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Model>())
.BuildSessionFactory();
});
services.AddScoped<NHibernate.ISession>(factory =>
factory
.GetServices<NHibernate.ISessionFactory>()
.First()
.OpenSession()
);
Run Code Online (Sandbox Code Playgroud)
我的问题是-例如,如何在存储库类中准确传递Session或SessionFactory实例?