views:

527

answers:

2

Hello! I'm using fluentnhibernate with PostgreSQL. Fluentnhibernate is last version. PosrgreSQL version is 8.4. My code for create ISessionFactory:

public static ISessionFactory CreateSessionFactory()
{
        string connectionString = ConfigurationManager.ConnectionStrings["PostgreConnectionString"].ConnectionString;
        IPersistenceConfigurer config = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString);

        FluentConfiguration configuration = Fluently
            .Configure()
            .Database(config)
            .Mappings(m =>
                m.FluentMappings.Add(typeof(ResourceMap))                                    
                                .Add(typeof(TaskMap))
                                .Add(typeof(PluginMap)));
        var nhibConfig = configuration.BuildConfiguration();
        SchemaMetadataUpdater.QuoteTableAndColumns(nhibConfig);
        return configuration.BuildSessionFactory();
}

When I'm execute code at line SchemaMetadataUpdater.QuoteTableAndColumns(nhibConfig); throw error: System.NotSupportedException: Specified method is not supported. Help me, please! I'm very need for solution. Best regards

A: 

Try this:

public static ISessionFactory CreateSessionFactory()
{
        string connectionString = ConfigurationManager.ConnectionStrings["PostgreConnectionString"].ConnectionString;
        IPersistenceConfigurer config = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString);

        FluentConfiguration configuration = Fluently
            .Configure()
            .Database(config)
            .Mappings(m =>
                m.FluentMappings.Add(typeof(ResourceMap))                                    
                                .Add(typeof(TaskMap))
                                .Add(typeof(PluginMap)));
        configuration.ExposeConfiguration(x => x.SetProperty("hbm2ddl.keywords", "auto-quote"));
        return configuration.BuildSessionFactory();
}
Jamie Ide
A: 
  1. SchemaMetadataUpdater.QuoteTableAndColumns(nhibConfig);
  2. configuration.ExposeConfiguration(x => x.SetProperty("hbm2ddl.keywords", "auto-quote"));

I tried above both. It didn't work for me with latest Fluent NHibernate(5f7adcd) and latest postgresql 8.4. Those two are probably on mute by Fluent NHibernate. If you use NHibernate and HBM without Fluent, it'd worked for you.

In order to explicitly ask Fluent NHibernate to generate Quoted Identifier for table and column, I monkey patched two files in Fluent NHibernate source to force it to work for postgresql. (If you don't need the same build for other databases)

namespace: FluentNHibernate.MappingModel.Output

  1. Add "Quote" to table name at XmlClassWriter.cs

    if (classMapping.HasValue(x => x.TableName))
      classElement.WithAtt("table", new System.Text.StringBuilder().Append("\"").Append(classMapping.TableName).Append("\"").ToString());
    
  2. Add "Quote" to column name at XmlColumnWriter.cs

    if (columnMapping.HasValue(x => x.Name))
      element.WithAtt("name", new System.Text.StringBuilder().Append("\"").Append(columnMapping.Name).Append("\"").ToString());
    

This works like charm so far. Get source at http://github.com/jagregory/fluent-nhibernate and build your own with above updates.

stoto
PostgreSQL considered "Quoted" as case sensitive, non-quoted indentifiers are case insensitive but all folded to lower case, meaning if you don't expect Fluent NHibernate to generate case sensitive "Quoted" query for you, you can just rename ALL your tables and columns to lower case names, which will make you feel weird next time you look at them. :]
stoto