Felice Pollano Blog

The Official Fatica Labs Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Disclaimer

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

© Copyright 2010

NHModeller - Keywords Name as Field Names

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)
);
 
 

 

	

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: NHModeller
Posted by Felice on Friday, May 01, 2009 6:13 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments are closed