2010-03-13 32 views
10

Tôi đang sử dụng fluentnhibernate với PostgreSQL. Fluentnhibernate là phiên bản cuối cùng. Phiên bản PosrgreSQL là 8.4. Mã của tôi cho tạo ISessionFactory:Lưu loát NHibernate và PostgreSQL, SchemaMetadataUpdater.QuoteTableAndColumns - System.NotSupportedException: Phương thức được chỉ định không được hỗ trợ

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

Khi tôi thực thi mã tại SchemaMetadataUpdater.QuoteTableAndColumns dòng (nhibConfig); lỗi ném: System.NotSupportedException: Phương thức đã chỉ định không được hỗ trợ. Làm ơn giúp tôi với! Tôi rất cần giải pháp. Trân trọng

Trả lời

8

Hãy thử điều này:

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

Làm mát điều này ('configuration.ExposeConfiguration ...') cũng hoạt động cho MS SQL (trong trường hợp có một số thắc mắc). – Nux

+2

câu trả lời quá cũ, vẫn còn tươi giải pháp;) Tôi ngưỡng mộ NHibernate nhất quán ... –

2
  1. SchemaMetadataUpdater.QuoteTableAndColumns (nhibConfig);
  2. cấu hình.ExposeConfiguration (x => x.SetProperty ("hbm2ddl.keywords", "tự động trích dẫn"));

Tôi đã thử trên cả hai. Nó không làm việc cho tôi với Fluent NHibernate mới nhất (5f7adcd) và postgresql 8.4 mới nhất. Hai thiết bị này là có thể là khi tắt tiếng bằng chế độ Fluent NHibernate. Nếu bạn sử dụng NHibernate và HBM mà không có Fluent, nó sẽ làm việc cho bạn.

Để hỏi một cách rõ ràng thành thạo NHibernate để tạo Quoted Định danh cho bảng và cột, Tôi khỉ vá hai tập tin trong nguồn thạo NHibernate để buộc nó để làm việc cho postgresql. (Nếu bạn không cần phải cùng xây dựng cho cơ sở dữ liệu khác)

namespace: FluentNHibernate.MappingModel.Output

  1. Add "Trích dẫn" để tên bảng tại XmlClassWriter.cs

    if (classMapping.HasValue(x => x.TableName)) 
        classElement.WithAtt("table", new System.Text.StringBuilder().Append("\"").Append(classMapping.TableName).Append("\"").ToString()); 
    
  2. Thêm "Trích dẫn" vào tên cột tại XmlColumnWriter.cs

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

Điều này làm việc như sự quyến rũ cho đến nay. Nhận nguồn tại số http://github.com/jagregory/fluent-nhibernate và tạo nguồn của riêng bạn bằng các bản cập nhật ở trên.

+0

PostgreSQL coi là "trích dẫn" là trường hợp nhạy cảm, không được trích dẫn indentifiers là trường hợp insensitive nhưng tất cả gấp đến trường hợp thấp hơn, có nghĩa là nếu bạn không mong đợi Lưu loát NHibernate để tạo ra truy vấn "Quoted" nhạy cảm với trường hợp, bạn chỉ cần đổi tên TẤT CẢ các bảng và cột thành các tên chữ thường, điều này sẽ khiến bạn cảm thấy kỳ lạ khi bạn nhìn vào chúng. :] – stoto

1

Tạo quy ước đặt tên tùy chỉnh của bạn, ghi đè quy ước tên cột để bao gồm báo giá.

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); 
    } 
} 
Các vấn đề liên quan