本人刚接触NHibernate 做了一个小demo 代码如下 private void Form1_Load(object sender, EventArgs e)
{
Configuration cfg = new Configuration().Configure();
ISessionFactory Factory = cfg.BuildSessionFactory();
ISession session = Factory.OpenSession();
IQuery query = session.CreateQuery("from Login");
IList<Login> list = query.List<Login>(); foreach (Login login in list)
{
MessageBox.Show(login.Name);
}
}运行 没问题。
接下来的 我将代码小小改动了一下public class Nhelp
{
public IList<T> Query<T>()
{
Configuration cfg = new Configuration().Configure();
ISessionFactory Factory = cfg.BuildSessionFactory();
ISession session = Factory.OpenSession();
IQuery query = session.CreateQuery("from login");
IList<T> list = query.List<T>();
return list;
}
}
写了一个 Nhelp 类
调用 private void Form1_Load(object sender, EventArgs e)
{
Nhelp help = new Nhelp();
IList<Login> list = help.Query<Login>();
foreach (Login login in list)
{
MessageBox.Show(login.Name);
}
}只是做了一个简单的封装,运行
在 Nhelp 类的 IQuery query = session.CreateQuery("from login");
出 报错 login is not mapped [from login]
错误代码
未处理 NHibernate.Hql.Ast.ANTLR.QuerySyntaxException
Message="login is not mapped [from login]"
Source="NHibernate"
QueryString="from login"
StackTrace:
在 NHibernate.Hql.Ast.ANTLR.SessionFactoryHelperExtensions.RequireClassPersister(String name)
在 NHibernate.Hql.Ast.ANTLR.Tree.FromElementFactory.AddFromElement()
在 NHibernate.Hql.Ast.ANTLR.Tree.FromClause.AddFromElement(String path, IASTNode alias)
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.CreateFromElement(String path, IASTNode pathNode, IASTNode alias, IASTNode propertyFetch)
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElement()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElementList()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromClause()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.unionedQuery()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.query()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectStatement()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.statement()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
在 NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(String collectionRole)
在 NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
在 NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary`2 replacements, Boolean shallow)
在 NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
在 NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryString, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.HQLStringQueryPlan.CreateTranslators(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
在 NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
在 NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)
在 demo.Nhelp.Query[T]() 位置 D:\Projects\WindowApp\demo\Nhelp.cs:行号 22
在 demo.Form1.Form1_Load(Object sender, EventArgs e) 位置 D:\Projects\WindowApp\PanasonicMES\PanasonicMES\Form1.cs:行号 39
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 PanasonicMES.Program.Main() 位置 D:\Projects\WindowApp\demo\Program.cs:行号 17
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException: 实在不知道 错在何处 求解
{
Configuration cfg = new Configuration().Configure();
ISessionFactory Factory = cfg.BuildSessionFactory();
ISession session = Factory.OpenSession();
IQuery query = session.CreateQuery("from Login");
IList<Login> list = query.List<Login>(); foreach (Login login in list)
{
MessageBox.Show(login.Name);
}
}运行 没问题。
接下来的 我将代码小小改动了一下public class Nhelp
{
public IList<T> Query<T>()
{
Configuration cfg = new Configuration().Configure();
ISessionFactory Factory = cfg.BuildSessionFactory();
ISession session = Factory.OpenSession();
IQuery query = session.CreateQuery("from login");
IList<T> list = query.List<T>();
return list;
}
}
写了一个 Nhelp 类
调用 private void Form1_Load(object sender, EventArgs e)
{
Nhelp help = new Nhelp();
IList<Login> list = help.Query<Login>();
foreach (Login login in list)
{
MessageBox.Show(login.Name);
}
}只是做了一个简单的封装,运行
在 Nhelp 类的 IQuery query = session.CreateQuery("from login");
出 报错 login is not mapped [from login]
错误代码
未处理 NHibernate.Hql.Ast.ANTLR.QuerySyntaxException
Message="login is not mapped [from login]"
Source="NHibernate"
QueryString="from login"
StackTrace:
在 NHibernate.Hql.Ast.ANTLR.SessionFactoryHelperExtensions.RequireClassPersister(String name)
在 NHibernate.Hql.Ast.ANTLR.Tree.FromElementFactory.AddFromElement()
在 NHibernate.Hql.Ast.ANTLR.Tree.FromClause.AddFromElement(String path, IASTNode alias)
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.CreateFromElement(String path, IASTNode pathNode, IASTNode alias, IASTNode propertyFetch)
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElement()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElementList()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromClause()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.unionedQuery()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.query()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectStatement()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.statement()
在 NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
在 NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(String collectionRole)
在 NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
在 NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary`2 replacements, Boolean shallow)
在 NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
在 NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryString, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.HQLStringQueryPlan.CreateTranslators(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.HQLStringQueryPlan..ctor(String hql, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
在 NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
在 NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
在 NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)
在 demo.Nhelp.Query[T]() 位置 D:\Projects\WindowApp\demo\Nhelp.cs:行号 22
在 demo.Form1.Form1_Load(Object sender, EventArgs e) 位置 D:\Projects\WindowApp\PanasonicMES\PanasonicMES\Form1.cs:行号 39
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 PanasonicMES.Program.Main() 位置 D:\Projects\WindowApp\demo\Program.cs:行号 17
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException: 实在不知道 错在何处 求解
解决方案 »
- 多线程Datagridview问题
- 一个自定义控件 如何通过单击事件 改变背景颜色 再单击又恢复成默认颜色?
- 帮个忙,从datagrid中查找与设置条件相符的行使之加亮显示,还有就是筛选出与条件相符的一些行?
- winform,请问我自己自定义的控件dll文件怎么样不主程序放到一个目录下而是放到新建一个文件夹
- 关于操作和分析大容量的xml?
- 一个小问题,求一个图像方法
- 【求助】读取文本文件冲突
- 如何用vs2003导出不带.cs文件的asp.net程序?谢谢!
- 谁有C#命名和书写规范,共享一下看看。
- C#想实现图片切换同时执行播放操作但是无反应
- ★★类似酷我音乐盒设计小总结(C#版)
- 使用itextsharp导出pdf,如何使多页有同一表头,表头具有嵌套。
一个数据库有一个 SessionFactory 就行了,不要每次重建,Session 用完要关闭 不明白,相同的代码 在窗体里面 没问题 ,单独用一个类封装起来 就不行
这个和SessionFactory 没关系吧
区分大小写吗?
IQuery query = session.CreateQuery("from Login");
ISessionFactory Factory = cfg.BuildSessionFactory();这是没必要的,浪费资源。在应用程序启动的时候配置一次就可以了。session 用完之后要关闭: session.Close,建议把 session 放在 using 语句里。