3

I'm using NHibernate mapping by code with naming conventions to map my entities. Now I have following error

SQL error - Incorrect syntax near the keyword 'User'.:

I know this is reserved word and I'm wonder how can I use this name (User) as entity name in mapping by conventions.

BobRock
  • 3,477
  • 3
  • 31
  • 48

3 Answers3

10

You can use it by wrapping it around square brackets as it is a reserved keyword in SQL SERVER:

[User]

The word user is a reserved word in SQL Server. If you need to use it as a column name, put brackets around it. This goes for all table names and other user-defined names that happen to collide with keywords,

Example:

Select * from tbl where [User] = 'xyz'
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
6

The correct way to deal with this in NHibernate is using SQL Quoted Identifiers.

Just wrap the table or column name in backticks and the Dialect will take care of using the right symbol for your database.

To make things easier, NHibernate provides a configuration setting that does this for you: set hbm2ddl.keywords="auto-quote" in an xml config file, or call AutoQuoteKeywords() when using loquacious.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
2

If the brackets don't work, try this:

"`User`"
Crowlix
  • 1,269
  • 9
  • 19