Reading tables with nHibernate that have SQL keywords as names

With nHibernate, if the name of the table that you are trying to map to happens to have a name which is an SQL Server keyword (i.e. User is a common one), then nHibernate will barf with a syntax error.

The way to get around this in your class map is to do as follows (for a table named User, mapped to a class named User). Notice the square brackets around the table name.

public class UserMap: ClassMap<User>
{
    public UserMap()
    {
        Table("[User]");
        ...
    }
}

If you know much SQL then this might have been obvious, but if not then you’d never get there! Of course you could always just not name your tables with SQL keywords, but if you are developing for legacy systems you may not have a choice…

Follow

Get every new post delivered to your Inbox.

Join 66 other followers