I have NHibernate XML mapping files that work wonderfully in MSSQL Databases. An example of a table is:
<class name="Worm" table="`Worms`" schema="`dbo`">
Now I need to use the exact same mapping file (unchanged) to generate a MariaDB (or MySQL) database. Clearly, such databases do not have schemas. So, what I'm trying to do is to create a naming convention so that the 'schema' becomes the prefix of a table, e.g. 'dbo_Worm'.
I've tried using the
var schemaUpdate = new NHibernate.Tool.hbm2ddl.SchemaUpdate(configuration);
by adding a custom Naming Strategy class into the 'configuration'. For now my custom class does nothing: just throws NotImplementedExceptions():
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();
}
}
The reasons are two:
Having tried everything and searched everywhere I'm turning to you for assistance. Can anyone help me
Any hints would be greatly appreciated!
Finally found a solution:
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;
}
}