1

I have an exception "GenericAdoException was unhandled" could not execute query while i was trying to do the nhibernate example on this line:

var users = session.CreateCriteria<User>().List<User>();

Inner exception is: incorrect syntax near 'User'

My Main method on 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:

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:

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:

<?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?

1
  • in hbm.xml, <class name="User"> should be <class name="User" table="Users"> Commented Dec 10, 2013 at 15:44

2 Answers 2

3

User is a reserved word in sql, you need to add [] around user or choose a different name for your object. Besides that your table name is Users, instead of user from your mapping.

Sign up to request clarification or add additional context in comments.

Comments

0

First, check connecting database name, use correct database name in connection string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.