The official Fatica Labs Blog! RSS 2.0
# Friday, May 01, 2009

When an field name conflict with some database dialect keywords, NHibernate offer a syntax in the mapping to escape the identifier so that the mapping can behave properly: quote the identifier with the "`" sign.

Unfortunately the dialect implementer is not obliged to espose a list of keywords that needs to be escaped, so I decided to put something in the NHModeller syntax to achieve the same results.

This is the sample entity:

 

NHModel{
Entity From 
{
@Select:int
@Count:int  
Another:string(30)
} in @From
}
The sign @ forces the correct escape generation in the mapping:
 
 <?xml version='1.0' ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyTest" namespace="MyTest.Entities">
<class name="From" table="`From`">
<id name="Id" column="Id" access="field.camelcase-underscore">
<generator class="native" />
</id>
<property name="Select" column="`Select`" type="System.Int32" not-null="true" />
<property name="Count" column="`Count`" type="System.Int32" not-null="true" />
<property name="Another" column="Another" type="String" not-null="true" length="30" />
</class>
</hibernate-mapping>
and will issue   the correct db script ( ie MsSql2005 ):
 
 
if exists (select * from dbo.sysobjects where id = object_id(N'[From]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [From];
create table [From] (
Id INT IDENTITY NOT NULL,
[Select] INT not null,
[Count] INT not null,
Another NVARCHAR(30) not null,
primary key (Id)
);
 
 

 

	

 

Friday, May 01, 2009 5:13:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
NHModeller
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Felice Pollano
Sign In
Statistics
Total Posts: 67
This Year: 41
This Month: 2
This Week: 0
Comments: 36
This blog visits
Locations of visitors to this page
All Content © 2010, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano