10

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

Sean Carpenter
  • 7,681
  • 3
  • 37
  • 38
Vyacheslav
  • 101
  • 1
  • 3

3 Answers3

9

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
  • 48,427
  • 16
  • 81
  • 117
  • Cool this (`configuration.ExposeConfiguration...`) also works for MS SQL (in case some wonders). – Nux Sep 09 '11 at 12:51
2
  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.

sth
  • 222,467
  • 53
  • 283
  • 367
stoto
  • 364
  • 1
  • 4
  • 10
  • 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 Apr 20 '10 at 00:48
1

Create your custom naming convention, override the column name convention to include quote.

var fluentConfig = Fluently.Configure(new     Configuration().SetNamingStrategy(PostgreNamingStragegy.Instance))

internal class PostgreNamingStragegy: INamingStrategy
    {
    private static readonly INamingStrategy ImprovedNamingStrategy = NHibernate.Cfg.ImprovedNamingStrategy.Instance;

    private static PostgreNamingStragegy_postgreNamingStrategy;
    public static INamingStrategy Instance
    {
        get { return _postgreNamingStrategy?? (_postgreNamingStrategy= new PostgreNamingStragegy()); }
    }

    protected PostgreNamingStragegy()
    {
    }

    public string ClassToTableName(string className)
    {
        return ImprovedNamingStrategy.ClassToTableName(className);
    }

    public string ColumnName(string columnName)
    {
        return "\"" + columnName + "\"";
    }

    public string LogicalColumnName(string columnName, string propertyName)
    {
        return ImprovedNamingStrategy.LogicalColumnName(columnName, propertyName);
    }

    public string PropertyToColumnName(string propertyName)
    {
        return ImprovedNamingStrategy.PropertyToColumnName(propertyName);
    }

    public string PropertyToTableName(string className, string propertyName)
    {
        return ImprovedNamingStrategy.PropertyToTableName(className, propertyName);
    }

    public string TableName(string tableName)
    {
        return ImprovedNamingStrategy.TableName(tableName);
    }
}
Dipen Lama
  • 147
  • 1
  • 5