NHibernate - Force escaping on Table Names
Asked Answered
D

1

6

Are there any good examples of how to use this (NHibernate.Criterion.IdentifierEqExpression) online? I couldn't find any. I'm a little confused about what you are supposed to pass into the constructor.

I pass in an int32 of 1 and I keep thinking my test should basically do a "where id = 1" type of query and instead it blows up with "where id = ?" and something about positional parameters. If that's not what is supposed to be passed into the constructor ... what is?

Real Issue
When I look at SQL output it seems to be working correctly except for the fact my table is named User and NHibernate isn't enclosing it like [User]. Any way to force this?

Dulcie answered 24/3, 2009 at 21:17 Comment(2)
Can you post an example of your criteria expression please?Kessler
I don't see the relationship between IdentifierEqExpression and escaping the table name... please explain a bit moreRora
G
15

Specify the table name as `User`. For example:

(HBM)
<class name="User" table="`User`">

(Fluent)
public UserMap()
{
    WithTable("`User`");
    ...

(Mapping By Code)
public UserMap()
{
    Table("`User`");
    ...

Similarly, with columns you'll have to do something like:

Map(x => x.IsCurrent, "`Current`");

Oh the joys of working with legacy DBs.

Globular answered 26/3, 2009 at 3:41 Comment(4)
You can use the backtick (`) to escape names in a database independent manner ([] are SQL Server specific).Gamete
Good point, I saw he was using [] and didn't even think about it. I'll update my post in the interests of best practices.Globular
Cool. Had no idea back-tick was the standard. Is that something Hibernate just borrowed from MySQL, and then parses out to make it database independent? Too bad the dialect settings don't take care off this for you.Dulcie
@StuartChilds: thnx for this helped very muchOuzo

© 2022 - 2024 — McMap. All rights reserved.