Mysql 启用hbm2ddl.keywords=在Fluent NHibernate中自动引用
我制作了一个微型软件工具,可以显示或运行NHibernate生成的SQL。我做这个是因为 我有一个问题:当我生成SQL时,我总是将臭名昭著的Mysql 启用hbm2ddl.keywords=在Fluent NHibernate中自动引用,mysql,sql,nhibernate,fluent-nhibernate,quotes,Mysql,Sql,Nhibernate,Fluent Nhibernate,Quotes,我制作了一个微型软件工具,可以显示或运行NHibernate生成的SQL。我做这个是因为 我有一个问题:当我生成SQL时,我总是将臭名昭著的索引列不带引号,因为我需要.AsList()映射。这使我无法运行SQL 理论上,如果我有一个NHibernate的XML配置,我可以使用hbm2ddl.keywordstag,但不幸的是,由于我的工具是为多个环境设计的DBA支持工具,我必须使用编程方法 我的方法(冗余)如下: private static Configuration BuildNHConfi
索引
列不带引号,因为我需要.AsList()
映射。这使我无法运行SQL
理论上,如果我有一个NHibernate的XML配置,我可以使用hbm2ddl.keywords
tag,但不幸的是,由于我的工具是为多个环境设计的DBA支持工具,我必须使用编程方法
我的方法(冗余)如下:
private static Configuration BuildNHConfig(string connectionString, DbType dbType, out Dialect requiredDialect)
{
IPersistenceConfigurer persistenceConfigurer;
switch (dbType)
{
case DbType.MySQL:
{
persistenceConfigurer =
MySQLConfiguration
.Standard
.Dialect<MySQL5Dialect>()
.Driver<MySqlDataDriver>()
.FormatSql()
.ShowSql()
.ConnectionString(connectionString);
requiredDialect = new MySQL5Dialect();
break;
}
case DbType.MsSqlAzure:
{
persistenceConfigurer = MsSqlConfiguration.MsSql2008
.Dialect<MsSqlAzure2008Dialect>()
.Driver<SqlClientDriver>()
.FormatSql()
.ShowSql()
.ConnectionString(connectionString);
requiredDialect = new MsSqlAzure2008Dialect();
break;
}
default:
{
throw new NotImplementedException();
}
}
FluentConfiguration fc = Fluently.Configure()
.Database(persistenceConfigurer)
.ExposeConfiguration(
cfg => cfg.SetProperty("hbm2ddl.keywords", "keywords")
.SetProperty("hbm2ddl.auto", "none"))
.Mappings(
m => m.FluentMappings.AddFromAssemblyOf<NHibernateFactory>());
Configuration ret = fc.BuildConfiguration();
SchemaMetadataUpdater.QuoteTableAndColumns(ret);
return ret;
}
...
public static void GenerateSql(MainWindowViewModel viewModel)
{
Dialect requiredDialect;
Configuration cfg = BuildNHConfig(viewModel.ConnectionString, viewModel.DbType.Value, out requiredDialect);
StringBuilder sqlBuilder = new StringBuilder();
foreach (string sqlLine in cfg.GenerateSchemaCreationScript(requiredDialect))
sqlBuilder.AppendLine(sqlLine);
viewModel.Sql = sqlBuilder.ToString();
}
其中未引用索引列
问题是:给定NHibernate的编程和流畅配置,我如何告诉NHibernate引用由
GenerateSchemaCreationScript
导出的SQL中的任何保留字?我找到了一个解决方法:当我生成更新脚本时(使用hbm2ddl.auto=update
运行的脚本)脚本被正确引用
这个问题已经讨论过了,根据我的发现,它是在FNH中硬编码的(ToManyBase.cs
,方法public T AsList()
)
由于更新脚本是一个在空数据库上完美工作的创建脚本,因此更改代码以在空数据库上生成更新脚本应等同于生成创建脚本
这只是因为我想自己生成脚本。NHibernate中可能有一个bug,它只在调用
GenerateSchemaCreationScript
时激活,而在让您的SessionFactory
为您构建DB时不激活我正在考虑使用约定。任何名为Index的列都将重命名为。。。例如,ListIndex
create table `OrderHistoryEvent` (Id BIGINT NOT NULL AUTO_INCREMENT, EventType VARCHAR(255) not null, EventTime DATETIME not null, EntityType VARCHAR(255), Comments VARCHAR(255), Order_id VARCHAR(255), Index INTEGER, primary key (Id))