Fluent NHibernate - Incorrect syntax near the keyword 'Group'

I have to share this. I spent a good amount of time searching around Fluent NHibernate for a way to escape columns in the generated SQL while still using AutoMapping.

My initial configuration looked like:

Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(
c => c.Is(ConfigurationManager.ConnectionStrings["Something"].ConnectionString)))
.Mappings(x => x.AutoMappings.Add(AutoPersistenceModel.MapEntitiesFromAssemblyOf()
.WithSetup(convention =>
{
convention.FindIdentity = p => p.Name == p.DeclaringType.Name + "Id";
convention.GetComponentColumnPrefix = type => type.Name + "Id";
} )
.BuildSessionFactory()

This works great, but I had a property in my class (SomeType) that is called Group. This was causing the SqlException: Incorrect syntax near the keyword 'Group' (others would be like: Incorrect syntax near the keyword 'User' or Incorrect syntax near the keyword 'Table').

The solution I found was to use the Fluent NHibernate ConventionDiscovery + ConvensionBuilder to always add a column name possibility (to use in SQL) which would be escaped by [] for every property. Why not? This is SQL 2005 I'm working with, right?

So the final solution looked like:

Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(
c => c.Is(ConfigurationManager.ConnectionStrings["Something"].ConnectionString)))
.Mappings(x => x.AutoMappings.Add(AutoPersistenceModel.MapEntitiesFromAssemblyOf()
.WithSetup(convention =>
{
convention.FindIdentity = p => p.Name == p.DeclaringType.Name + "Id";
convention.GetComponentColumnPrefix = type => type.Name + "Id";
} )
.ConventionDiscovery.Setup(c => c.Add(ConventionBuilder.Property.Always(
s => s.ColumnNames.Add("[" + s.Property.Name + "]"))))))
.BuildSessionFactory()

Comments

Unknown said…
Nice article..
Thank you for sharing,
keep posting and update article or informatin more..
visit Khasiat Obat Ace Maxs
Unknown said…
Thank you for sharing articles were very nice and useful like this ..
we liked it very much, I hope you can post or renew it again.
good luck,,
visit Obat Herbal Penyakit Leukemia
Unknown said…
How do your activities today? hopefully smooth and fun
Thank you for sharing about the articles and information with us,,
we really liked and listened attentively ..
it is very nice
visit Obat Herbal Kanker Tulang
Unknown said…
Thanks for sharing.. it is very good and amazing.
good job, gan. keep posting and sharing the article more
visit Obat Herbal Kanker Kelenjar Getah Bening
Unknown said…
very good article and interesting, this may be the first step to your success.
Keep posting and updating information or other article,,
Thank you
visit Obat Benjolan Di Belakang Telinga Yang Ampuh
Unknown said…
very nice article, interesting and easy to understand, this may be the first step to your success.
Keep posting and updating information or other article,,
Thank you
visit Obat Herbal Diabetes Melitus Yang Ampuh
Unknown said…
I am not good at making information or a very good article, but this article makes me become motivated to make work better.
So, thank you for sharing the article very interesting and very good and this
visit Obat Benjolan Di Rahim Ampuh Dan Aman
Unknown said…
Still listening article you serve to make me be inspired in makes work creating articles or information that could be useful,, interesting and easily understood.
Thank you very much for sharing and I wait for your next post.
survivors welcome the new year
visit Obat Herbal Gagal Ginjal Tanpa Cuci Darah
Unknown said…
My visit today on your website very pleasant ..
very nice article and the contents of the article are of sufficient quality.
hopefully more successful and hopefully this article can be useful for the visitors.
Thank you
visit | Obat Herbal Glaukoma Ampuh Dan Aman
Unknown said…
This is article that you created and you have, very nice, the content easy to understand it and used as a reference of quality.
thank you for sharing.,, Keep posting and I wait for your next post
visit | Jenis Makanan Penyebab Hipertensi Pada Ibu Hamil
Unknown said…
This article contains information that is very useful for all readers, especially my own. I wait for other information that is not less interesting
Thank you for sharing, Don't stop for the posting article or information more
visit | Kenali Penyebab Penyakit Stroke Dan Cara Mengatasinya
Unknown said…
I don't know how often I visit this blog..
But, thank you for sharing your article.. Keep posting
visit | Apakah Ginjal Bocor Bisa Sembuh Total
Unknown said…
I found something in this article., a very nice design, the contents of the article were quite good and quality..
it is very suitable as a good reference.
Thank you very much for sharing with us
visit | Penyebab Ambeien Dan Cara Mengatasinya Tanpa Operasi
Unknown said…
a very nice design, the contents of the article were quite good and quality..
it is very suitable as a good reference.
Thank you very much for sharing with us
visit | Waspadai Gejala Awal Glaukoma Penyebab Utama Kebutaan

Popular posts from this blog

Require comment / message on all SVN commits for Visual SVN Server

Deleting Cookies (or Managing Cookie Domains) in ASP.NET