BobRock
BobRock

Reputation: 3467

using reserved word as entity name

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.

Upvotes: 3

Views: 2667

Answers (3)

Diego Mijelshon
Diego Mijelshon

Reputation: 52735

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.

Upvotes: 6

Vishal Suthar
Vishal Suthar

Reputation: 17194

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'

Upvotes: 10

Crowlix
Crowlix

Reputation: 1269

If the brackets don't work, try this:

"`User`"

Upvotes: 2

Related Questions