views:

43

answers:

1

I am setting up Castle Active Record to access a legacy database on an Microsoft SQL Server. The table in question has a column named function. When I attampt to load a record it gives me this error:

Incorrect syntax near the keyword 'function'

This error comes about because the SQL query nhibernate generates attempts to access the column named function, which happens to be a restricted keyword. The fix is to wrap the column name in square braces ([function]) in the SQL statement. Is it possible to tell nhibernate to do this in its autogenerated SQL?

+1  A: 

This will help you:

http://quomon.com/question-How-do-I-handle-database-reserved-words-in-NHibernate-1148.aspx

Example where table is a reserved keyword:

<class name="User" table="[user]" lazy="false">

I think the same applies for column names. Wrap them in square braces.

Leniel Macaferi
the portable way to express this is using backticks, see duplicate question reference.
Mauricio Scheffer
Not always. As you see in that question the user posted an answer that doesn't use backticks probably because using backticks would not work for him.
Leniel Macaferi
take a look at the times of the answers...
Mauricio Scheffer