[英]NHibernate GenericAdoException was unhandled could not execute query
I have an exception "GenericAdoException was unhandled" could not execute query while i was trying to do the nhibernate example on this line: 我有一个异常“GenericAdoException未处理”无法执行查询,而我正在尝试在此行上执行nhibernate示例:
var users = session.CreateCriteria<User>().List<User>();
Inner exception is: incorrect syntax near 'User' 内部异常是:'User'附近的语法不正确
My Main method on Program.cs: Program.cs上的我的主要方法:
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var cfg = new Configuration();
cfg.DataBaseIntegration(x =>
{
x.ConnectionString = "Server=.;database=NHibernateTryOutDB;uid=sa;pwd=Hasar2012";
x.Driver<SqlClientDriver>();
x.Dialect<MsSql2012Dialect>();
});
cfg.AddAssembly(Assembly.GetExecutingAssembly());
var sessionFactory = cfg.BuildSessionFactory();
using (var session = sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
var users = session.CreateCriteria<User>().List<User>();
foreach (User item in users)
{
Console.WriteLine("{0} {1}", item.Name, item.Surname);
}
tx.Commit();
}
Console.WriteLine("Press <Enter> to exit...");
Console.ReadLine();
}
}
}
I ve a table named Users: 我有一个名为Users的表:
USE [NHibernateTryOutDB]
GO
/****** Object: Table [dbo].[Users] Script Date: 12/10/2013 16:04:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Surname] [nvarchar](50) NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
My class names User.cs: 我的班级名称User.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class User
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
}
}
My .hbm.xm file that is User.hbm.xml: 我的.hbm.xm文件是User.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ConsoleApplication2"
namespace="ConsoleApplication2">
<class name="User">
<id name="ID">
<generator class="native"/>
</id>
<property name="Name"/>
<property name="Surname"/>
</class>
</hibernate-mapping>
How can i solve this issue? 我该如何解决这个问题?
User is a reserved word in sql, you need to add [] around user or choose a different name for your object. 用户是sql中的保留字,您需要在用户周围添加[]或为对象选择其他名称。 Besides that your table name is Users, instead of user from your mapping.
除此之外,您的表名是Users,而不是映射中的用户。
首先,检查连接数据库名称,在连接字符串中使用正确的数据库名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.