views:

33

answers:

1

I have a class like this:

public class test
{
    public virtual int Id { get; private set; }
    public virtual char testType { get; set; }
    public virtual char testType2 { get; set; }
}

When I create this, but don't specificy testType2, I want the value to be "null" in the database. Instead, the value is an empty char. One way that I found to force this to be null is to use char? for the type. However, this apparently doesn't work for strings (doesn't compile), and I really don't want to mess with nullable types. Is there any way to get fluent nhibernate to always save uninitialized variables as "null"?

Edit: uninitialized string variables are saved as "null" in the db. I still need a solution for char and int.

A: 
public class test
{
    public virtual int? Id { get; private set; }
    public virtual DateTime? SomeDate { get; private set; }
    public virtual char? testType { get; set; }
    public virtual char? testType2 { get; set; }
}

All you need to do is to add a ? after the type.

mhenrixon
I was hoping for a solution without using nullable types (that's what ? does).
Matthew Talbert
The problem is that if your C# does not support null you get problems anyway. I suppose it's possible to solve with some event listener but that seems like an awful lot of work for such a small thing.
mhenrixon
Well, the real answer is that there is no way to do it. It has to do with the fact that variables such as char are always initialized, therefore can never be null unless they are explicitly nullable (with ?). So I'm going to mark your answer as correct anyway.
Matthew Talbert

related questions