Search code examples
sql-servernhibernatecastle-activerecord

accessing a table where the column name is a reserved keyword


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?


Solution

  • 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.