Sie können dies mit einem UserType tun. Ich bin zu dem Schluss gekommen, dass Null-Strings in meinen Business-Klassen nutzlos (und lästig) sind, also konvertiere ich alle nullbaren String-Datenbankspalten in leere Strings und umgekehrt.
Fließender Gebrauch ist:
Map(x => x.MiddleName).CustomType(typeof(NullableString));
/// <summary>
/// UserType for string properties that are database nullable. Using this type
/// will substitue empty string for null when populating object properties
/// and null for empty string in database operations.
/// </summary>
/// <example>
/// Map(x => x.MiddleName).Length(30).Nullable().CustomType(typeof(NullableString));
/// </example>
public class NullableString : IUserType
{
public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (x == null || y == null)
{
return false;
}
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var valueToGet = NHibernateUtil.String.NullSafeGet(rs, names[0]);
return valueToGet ?? string.Empty;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var stringObject = value as string;
object valueToSet = string.IsNullOrEmpty(stringObject) ? null : stringObject;
NHibernateUtil.String.NullSafeSet(cmd, valueToSet, index);
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
public SqlType[] SqlTypes
{
get
{
return new[] { new SqlType(DbType.String)};
}
}
public Type ReturnedType
{
get { return typeof(string); }
}
public bool IsMutable
{
get { return false; }
}
}