Fluent NHibernate和PostgreSQL,SchemaMetadataUpdater.QuoteTableAndColumns-System.NotSupportedException:不支持指定的方法
我在PostgreSQL中使用fluentnhibernate。Fluentnhibernate是最新版本。PosrgreSQL版本是8.4。 创建ISessionFactory的我的代码:Fluent NHibernate和PostgreSQL,SchemaMetadataUpdater.QuoteTableAndColumns-System.NotSupportedException:不支持指定的方法,nhibernate,postgresql,fluent-nhibernate,Nhibernate,Postgresql,Fluent Nhibernate,我在PostgreSQL中使用fluentnhibernate。Fluentnhibernate是最新版本。PosrgreSQL版本是8.4。 创建ISessionFactory的我的代码: public static ISessionFactory CreateSessionFactory() { string connectionString = ConfigurationManager.ConnectionStrings["PostgreConnectionString"]
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();
}
当我在SchemaMetadataUpdater.QuoteTableAndColumns(nhibConfig)行执行代码时;抛出错误:System.NotSupportedException:不支持指定的方法。请帮帮我!我非常需要解决这个问题。
致以最诚挚的问候试试这个:
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();
}
FluentNHibernate.MappingModel.Output
if (classMapping.HasValue(x => x.TableName))
classElement.WithAtt("table", new System.Text.StringBuilder().Append("\"").Append(classMapping.TableName).Append("\"").ToString());
if (columnMapping.HasValue(x => x.Name))
element.WithAtt("name", new System.Text.StringBuilder().Append("\"").Append(columnMapping.Name).Append("\"").ToString());
并使用上述更新构建您自己的。创建自定义命名约定,覆盖列名约定以包含引号
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);
}
}
PostgreSQL将“Quoted”视为区分大小写,非Quoted标识符不区分大小写,但全部折叠为小写,这意味着如果您不希望Fluent NHibernate为您生成区分大小写的“Quoted”查询,您可以将所有表和列重命名为小写名称,这会让你下次看到它们时感到奇怪冷却此(configuration.ExposeConfiguration…
)也适用于MS SQL(以防万一)。