我有一个使用Nhibernate的应用。这是我第一个使用它的应用程序,所以我是新手。我正在尝试测试我的第一个get方法。
protected void Button1_Click(object sender, EventArgs e)
{
IList<Person> persons =
SessionManager
.SessionFactory
.GetCurrentSession()
.CreateCriteria(typeof(Person))
.List<Person>();
}
这是会话mgr代码:
public static partial class SessionManager
{
private static readonly ISessionFactory _sessionFactory;
static SessionManager()
{
Configuration cfg = new Configuration().Configure();
_sessionFactory =
Fluently
.Configure(cfg)
.Mappings(m => m.FluentMappings.AddFromAssembly(typeof(SessionManager).Assembly))
.BuildSessionFactory();
//CurrentSessionContext.Bind(_sessionFactory.GetCurrentSession());
}
public static ISession OpenSession()
{
return _sessionFactory.OpenSession();
}
public static ISessionFactory SessionFactory
{
get { return _sessionFactory; }
}
}
但每次我运行应用程序并单击btn。它会引发异常内部异常msg:
“'name'属性无效 - 值 'hibernate.current_session_context_class'根据其无效 数据类型'String' - 枚举约束失败。“}
这是我的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-16"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="hibernate.current_session_context_class">managed</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=localhost\MSSQLSERVERR2;Database=PersonSearch;Trusted_Connection=True</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="cache.use_minimal_puts">false</property>
<property name="use_outer_join">false</property>
</session-factory>
</hibernate-configuration>
最初,我没有把
<property name="hibernate.current_session_context_class">managed</property>
但它给了我一个错误:
未配置CurrentSessionContext(设置属性 current_session_context_class)!
答案 0 :(得分:0)
如果在Web应用程序中,请尝试以下操作:
<property name="current_session_context_class">managed_web</property>
如果在winform应用程序中,请尝试以下操作:
<property name="current_session_context_class">thread_static</property>