2010-03-13 3 views
10

Ich benutze fluentnhibernate mit PostgreSQL. Fluentnibernate ist die letzte Version. Die Version von PosrgreSQL ist 8.4. Mein Code für ISessionFactory erstellen:Fließend NHibernate und PostgreSQL, SchemaMetadataUpdater.QuoteTableAndColumns - System.NotSupportedException: Angegebene Methode wird nicht unterstützt

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(); 
} 

Wenn ich ausführen bin Code in Zeile SchemaMetadataUpdater.QuoteTableAndColumns (nhibConfig); throw error: System.NotSupportedException: Angegebene Methode wird nicht unterstützt. Hilf mir bitte! Ich brauche dringend eine Lösung. Mit freundlichen Grüßen

Antwort

8

Versuchen Sie folgendes:

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(); 
} 
+0

Cool dies ('configuration.ExposeConfiguration ...') funktioniert auch für MS SQL (falls einige Wunder). – Nux

+2

so alt Antwort, noch so frische Lösung;) Ich bewundere NHibernate Konsistenz ... –

2
  1. SchemaMetadataUpdater.QuoteTableAndColumns (nhibConfig);
  2. configuration.ExposeConfiguration (x => x.SetProperty ("hbm2ddl.keywords", "auto-quote"));

Ich versuchte oben beide. Es funktionierte nicht für mich mit dem neuesten Fluent NHibernate (5f7adcd) und dem neuesten PostgreSQL 8.4. Diese beiden sind wahrscheinlich auf durch Fluent NHibernate stumm. Wenn Sie NHibernate und HBM ohne Fluent verwenden, hat es für Sie funktioniert.

Um Fluent NHibernate explizit fragen Bezeichner in Anführungszeichen für Tabellen- und Spalten, ich Affe gepatcht zwei Dateien in Fluent NHibernate Quelle zu erzeugen, um es zu zwingen für postgresql zu arbeiten. (Wenn Sie brauchen nicht die gleiche Build für andere Datenbanken)

Namespace: FluentNHibernate.MappingModel.Output

  1. hinzufügen "Quote" zu Tabellenname bei XmlClassWriter.cs

    if (classMapping.HasValue(x => x.TableName)) 
        classElement.WithAtt("table", new System.Text.StringBuilder().Append("\"").Append(classMapping.TableName).Append("\"").ToString()); 
    
  2. Fügen Sie "Quote" zum Spaltennamen bei hinzu. XmlColumnWriter.cs

Das funktioniert so weit wie Charme. Holen Sie sich die Quelle unter http://github.com/jagregory/fluent-nhibernate und bauen Sie Ihre eigenen mit den oben genannten Updates.

+0

PostgreSQL als "zitiert" als Groß-und Kleinschreibung, nicht-zitierte Identifikatoren Groß-und Kleinschreibung, aber alle in Kleinbuchstaben, was bedeutet, wenn Sie nicht erwarten Fluent NHibernate, um die Groß- und Kleinschreibung der "Quoted" -Abfrage für Sie zu generieren, können Sie einfach alle Ihre Tabellen und Spalten in Kleinbuchstaben umbenennen, wodurch Sie sich beim nächsten Mal komisch fühlen werden. :] – stoto

1

Erstellen Sie Ihre benutzerdefinierte Namenskonvention, überschreiben Sie die Spaltennamenskonvention, um ein Zitat einzufügen.

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); 
    } 
}