使用 XML 映射向表添加架构前缀 - 将 MSSQL 数据库转换为 MySQL 时需要



我有NHibernate XML映射文件,这些文件在MSSQL数据库中运行良好。表的示例如下:

<class name="Worm" table="`Worms`" schema="`dbo`">

现在我需要使用完全相同的映射文件(不变(来生成 MariaDB(或 MySQL(数据库。显然,此类数据库没有架构。因此,我要做的是创建一个命名约定,以便"模式"成为表的前缀,例如"dbo_Worm"。

我试过使用

var schemaUpdate = new NHibernate.Tool.hbm2ddl.SchemaUpdate(configuration);

通过将自定义命名策略类添加到"配置"中。现在我的自定义类什么都不做:只是抛出 NotImplementExceptions((:

public class MyCustomNamingStrategy : INamingStrategy
{
public static MyCustomNamingStrategy Instance => new MyCustomNamingStrategy(); 
public string ClassToTableName(string className)
{
throw new NotImplementedException();
}
public string PropertyToColumnName(string propertyName)
{
throw new NotImplementedException();
}
public string TableName(string tableName)
{
throw new NotImplementedException();
}
public string ColumnName(string columnName)
{
throw new NotImplementedException();
}
public string PropertyToTableName(string className, string propertyName)
{
throw new NotImplementedException();
}
public string LogicalColumnName(string columnName, string propertyName)
{
throw new NotImplementedException();
}
}

原因有二:

  1. 我从未达到我的自定义命名策略的断点 首先是上课,所以我什至不知道这是否是要走的路。 它会给我任何关于"架构"的信息吗?我 不知道。。。
  2. 调用该工具的 SchemaUpdate 的代码完全忽略自定义命名策略并引发 MySQL 异常 声明未找到"dbo"数据库(呃...

尝试了一切并到处搜索后,我向您寻求帮助。 谁能帮我

  • 保留完全相同的 XML 映射文件,但
  • 生成以架构名称为前缀的表?

任何提示将不胜感激!

终于找到了一个解决方案:

public override void RemoveSchemas(NHibernate.Cfg.Configuration configuration)
{
foreach (var clsMapping in configuration.ClassMappings)
{
clsMapping.Table.Schema = null;
if ((clsMapping as NHibernate.Mapping.RootClass) != null) (clsMapping as NHibernate.Mapping.RootClass).CacheRegionName = null;
if (clsMapping.IdentityTable != null)
{
clsMapping.IdentityTable.Schema = null;
var identifier = clsMapping.IdentityTable.IdentifierValue as NHibernate.Mapping.SimpleValue;
if (identifier != null)
{
if(identifier?.IdentifierGeneratorProperties?.ContainsKey("schema") == true)
{
identifier.IdentifierGeneratorProperties["schema"] = null;
}
}
}
}
foreach (var colMapping in configuration.CollectionMappings)
{
colMapping.Table.Schema = null;
if (colMapping.CollectionTable != null) colMapping.CollectionTable.Schema = null;
colMapping.CacheRegionName = null;
}
}

最新更新