orchardcmsorchard-modules

Get full tablename for custom sql query in Orchard CMS


In Orchard CMS, what is the best way to get the full table name? I want to perform a custom SQL query (so not using IRepository<T>), but would need the full table name for this. I know I can use SchemaBuilder.TableDbName but to create the SchemaBuilder, I also need an IDataMigrationInterpreter, featurePrefix and formatPrefix. Where do I get those? The IDataMigrationInterpreter I can get via the dependency injection mechanism of Orchard, but where can I get the prefixes, knowing I'm developing a module and so can't hard-code the prefixes.


Solution

  • The tenant's table prefix (if any) is available in the injectable ShellSettings object. The table name that comes after that follows simple rules so you could hard-code that (since this is not dynamic, it only depends on the module's and the record's name).

    See the built-in Upgrade module for examples, e.g. Upgrade.Services.UpgradeService and Upgrade.Controllers.ContentPickerController.

    Example:

    var tablePrefix = _shellSettings.DataTablePrefix;
    var featurePrefix = "MyModule_MyFeature";
    if (string.IsNullOrWhiteSpace(tablePrefix)) {
        return $"{featurePrefix}_{type.Name}";
    }
    
    return $"{tablePrefix}_{featurePrefix}_{type.Name}";