我的Nhibernate测试项目中没有当前会话上下文设置错误
作者:互联网
我收到错误消息:
No CurrentSessionContext configured (set the property current_session_context_class).
我不确定该放在哪里,我有这个:
public class NhDbHelper
{
public NhDbHelper()
{
CreateSessionFactory();
}
private ISessionFactory _sessionFactory;
public ISessionFactory SessionFactory
{
get { return _sessionFactory; }
}
private void CreateSessionFactory()
{
_sessionFactory = Fluently
.Configure()
.Database((MsSqlConfiguration.MsSql2008 //
.ConnectionString(@"Server=.\SQLExpress;Database=abc;Uid=sa;Pwd=123;")
.ShowSql()))
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<UserMap>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
}
}
然后在我的存储库中,我只使用助手中的SessionFactory属性.
解决方法:
在“ Fluently”中,在“ .Mappings(—-)语句之前”,您需要指定CurrentSessionContext.为此,假设您在Web上下文中使用它,则应在“ .Mappings”行上方插入如下所示.(由于Fluent,我还修改了检索连接字符串的值:
private void CreateSessionFactory()
{
_sessionFactory = Fluently
.Configure()
.Database((MsSqlConfiguration.MsSql2008 //
.ConnectionString(c=>c.FromConnectionStringWithKey("abc"))
.ShowSql()))
.CurrentSessionContext("web")
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<UserMap>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
}
标签:console-application,nhibernate,c 来源: https://codeday.me/bug/20191102/1989408.html