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()
0 comments:
Post a Comment