default_schema and Formula

24 views
Skip to first unread message

Lorenzo Dieryckx

unread,
Apr 4, 2012, 8:47:57 AM4/4/12
to Fluent NHibernate
Hi,

I've noticed that the default_schema setting only works for generated
SQL.
user-provided SQL (in my case: via Map().Formula()) seems unaffected
by the default_schema setting.

Is there any way around this?
I've tried the naive approach by trying to read the default_schema
from the configuration in the mapping-class and then changing the SQL
in the .Formula() call to include the default_schema, but even
accessing the configuration from the mapping-file seems non-trivial.

Any help would be greatly appreciated!

Regards,
Lorenzo

Lorenzo Dieryckx

unread,
Apr 23, 2012, 5:57:37 AM4/23/12
to Fluent NHibernate
On Apr 4, 2:47 pm, Lorenzo Dieryckx <lorenzo.diery...@gmail.com>
wrote:
I solved this myself.

Since it seems impossible to access any configuration in the ClassMap
(and I didn't want to start reading config files manually there) and
since you can't have dependencies injected in the ClassMap via some
IoC framework (Classmaps need a parameterless constructor),
I had to do it the "old fashioned way" with a singleton class:

The singleton:

public class DefaultSchemaProvider
{
private static DefaultSchemaProvider _instance = null;
private string _defaultSchema = null;

public static DefaultSchemaProvider Instance
{
get
{
if (_instance == null)
_instance = new DefaultSchemaProvider();
return _instance;
}
}

public void SetDefaultSchema(string defaultSchema)
{
_defaultSchema = defaultSchema;
}

public string GetDefaultSchema()
{
return _defaultSchema;
}
}

During configuration:

var nhibernateCfg = new NHibernate.Cfg.Configuration();
nhibernateCfg.Configure(ConfigurationFile);

DefaultSchemaProvider.Instance.SetDefaultSchema(nhibernateCfg.GetProperty("default_schema"));

in the classmap:

public class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
var defaultSchema =
DefaultSchemaProvider.Instance.GetDefaultSchema() + ".";

Table("Customers");

Id(x => x.Id).Column("Customer_id");
Map(x => x.Description).Column("Name");
Component(c => c.CustomerType, c =>
{
c.Map(x => x.Id).Column("Customer_type");
c.Map(x => x.Description).Formula(
"(SELECT ct.value FROM " + defaultSchema +
"customer_types ct WHERE ct.field='description' AND ct.typeid =
Customer_type)");
});
HasMany(x =>
x.ArticleLinks).KeyColumn("VENR").Access.CamelCaseField(Prefix.Underscore);
}
}
Reply all
Reply to author
Forward
0 new messages