views:

287

answers:

1

We work with legacy database which are saving integer values to the varchar column. We need to map this column to Int32 property and it works well if data in database column are numeric or NULL.

But we have problems if column contains empty string instead of null - nhibernate throws error that it cannot convert it to integer.

Is it possible to configure class mapping to automatically convert all values where the exception raises to some default value (zero in this case)?

+2  A: 

It is probably possible to convert the values using an interceptor. You may get some advice by searching for "null value substitution".

But do you really have this need? You could map a private or read-only member for the varchar column and use a different property to control it.

private string _varcharField;

public string VarcharField
{
    get { return _varcharField; }
}

public int IntProperty
{
    get { [parse and return _varcharField]; }
    set { _varcharField = value.ToString(); }
}

In this example, _varcharField would be mapped in NHibernate using an access strategy.

Jamie Ide
Thanks, this is something I want to do if it cannot be done by mapping.
nihique