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:
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:
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
Thank you for sharing,
keep posting and update article or informatin more..
visit Khasiat Obat Ace Maxs
we liked it very much, I hope you can post or renew it again.
good luck,,
visit Obat Herbal Penyakit Leukemia
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
good job, gan. keep posting and sharing the article more
visit Obat Herbal Kanker Kelenjar Getah Bening
Keep posting and updating information or other article,,
Thank you
visit Obat Benjolan Di Belakang Telinga Yang Ampuh
Keep posting and updating information or other article,,
Thank you
visit Obat Herbal Diabetes Melitus Yang Ampuh
So, thank you for sharing the article very interesting and very good and this
visit Obat Benjolan Di Rahim Ampuh Dan Aman
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
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
thank you for sharing.,, Keep posting and I wait for your next post
visit | Jenis Makanan Penyebab Hipertensi Pada Ibu Hamil
Thank you for sharing, Don't stop for the posting article or information more
visit | Kenali Penyebab Penyakit Stroke Dan Cara Mengatasinya
But, thank you for sharing your article.. Keep posting
visit | Apakah Ginjal Bocor Bisa Sembuh Total
it is very suitable as a good reference.
Thank you very much for sharing with us
visit | Penyebab Ambeien Dan Cara Mengatasinya Tanpa Operasi
it is very suitable as a good reference.
Thank you very much for sharing with us
visit | Waspadai Gejala Awal Glaukoma Penyebab Utama Kebutaan