开发者

Nullable string property/column in nHibernate + Fluent

开发者 https://www.devze.com 2023-02-09 13:25 出处:网络
I\'m new开发者_开发问答 to nHibernate universe, so this question can be idiot .. I have a table with a nullable nvarchar column.

I'm new开发者_开发问答 to nHibernate universe, so this question can be idiot .. I have a table with a nullable nvarchar column. In the mapping i use this Map(c => c.Genero, "genero").Nullable(); In the property i use trim to set the value, so i have a private string and a public virtual string. When i do an select in this table i receive an runtime error in the setter of this property. I tryed to pass this property to Nullable, but i receive a compile-time error saying The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'.

How can i do this ?

Thanks for everyone !

UPDATE

If i use just the property with { get; set; } works normally, but i need to trim.


The problem may be that you can't trim a null string. Try:

public string Genero
{
    get { return _genero; }
    set { _genero = string.IsNullOrEmpty(value) ? value : value.Trim(); }
}

Depending on your mapping, this may cause the trimmed string to be written to the database when the session is flushed. It might be better to map the string as a private field and trim it in the getter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号