Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: forcing square brackets in schema export?

Is there a way to tell NHibernate to use square brackets for all table and column names (like [MyColumn]) when generating the SQL schema export for MS SQL Server? I have a legacy database that uses reserved names for certain columns and running the SQL script generated using NH throws an error because of it.

I want to avoid having to specify this separately for each column.

UPDATE: I'm using the correct dialect:

MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)

UPDATE 2: @UpTheCreek pointed me in the right direction - backticks, which helped me find the answer in the "NHibernate in Action" book (p. 76):

There is no way, apart from quoting all table and column names in backticks, to force NHibernate to use quoted identifiers everywhere.

like image 624
Igor Brejc Avatar asked Nov 05 '10 09:11

Igor Brejc


2 Answers

Easier approach:

SchemaMetadataUpdater.QuoteTableAndColumns(config)

(Before building SessionFactory)

That will quote all the reserved names automatically.

like image 138
Diego Mijelshon Avatar answered Sep 27 '22 15:09

Diego Mijelshon


Use backticks in your mapping files around the column names. NH should replace these with the correct character for your db dialect (in your case square brackets).

i.e. use:

<class name="SomeClass" table="`SomeTable`">

NB - It won't work with an apostrophe. The backtick is located top left on most keyboards.

like image 43
UpTheCreek Avatar answered Sep 27 '22 15:09

UpTheCreek