NHibernate: forcing square brackets in schema expo

2019-02-22 04:31发布

问题:

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.

回答1:

Easier approach:

SchemaMetadataUpdater.QuoteTableAndColumns(config)

(Before building SessionFactory)

That will quote all the reserved names automatically.



回答2:

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.



回答3:

You need to use (or write) the correct dialect for your database