Othman Fayed
Othman Fayed

Reputation: 135

nHibernate 4.1.4 is not quoting table name that matches reserved keyword

I'm using nHibernate 4.1.4 MappingByCode. My Dialect is

public class Dialect : NHibernate.Dialect.MsSql2012Dialect
{
    protected override void RegisterKeywords()
    {
        base.RegisterKeywords();
        RegisterKeyword("user");
    }
}

My config is setting config.SetProperty(Environment.Hbm2ddlKeyWords, "keywords");

I have a table named User.

SQL error is thrown that invalid table name User is invalid. nHibernate fails to wrap it in brackets.

Any ideas?

Upvotes: 1

Views: 226

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123861

Keywords are used for different purpose...

what we need is ad hoc table name escaping

5.3. SQL quoted identifiers

You may force NHibernate to quote an identifier in the generated SQL by enclosing the table or column name in back-ticks in the mapping document. NHibernate will use the correct quotation style for the SQL Dialect (usually double quotes, but brackets for SQL Server and back-ticks for MySQL).

<class name="LineItem" table="`Line Item`">
    <id name="Id" column="`Item Id`"/><generator class="assigned"/></id>
    <property name="ItemNumber" column="`Item #`"/>
    ...
</class>

I.e. in mapping, we need to escape table name:

"`user`"

Keywords, defined in configuration of the dialect, would help NH parser when working with custom sql statements. E.g. in formulas, subselects.

Upvotes: 1

Related Questions