Search code examples
mysqlnhibernatemappingschemahbm2ddl

Adding a Schema Prefix to a Table using XML Mapping - needed to convert a MSSQL Database to a MySQL


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:

  1. I've never reached the breakpoints of my MyCustomNamingStrategy class to begin with, so I don't even know if this is the way to go. Will it give me any information with regards to the 'schema'? I don't know...
  2. The code that calls the SchemaUpdate of the tool completely ignores the custom naming strategy and a MySQL Exception is thrown stating that no 'dbo' database is found (duh....)

Having tried everything and searched everywhere I'm turning to you for assistance. Can anyone help me

  • Keep the exact same XML Mapping File, yet
  • Produce tables prefixed with their schema names ?

Any hints would be greatly appreciated!


Solution

  • 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;
            }
        }