views:

20

answers:

1

How can I map a nullable value-type property as a component in NHibernate?

For example:

public struct PersonName
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    public PersonName(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

public class Person
{
    public PersonName? Name { get; set; }
}

public class PersonDbMap : ClassMap<Person>
{
     public PersonDbMap()
     {
         /* This part doesn't compile! */
         Component(x => x.Name,
             part =>
             {
                 part.Map(x => x.FirstName, "FirstName");
                 part.Map(x => x.LastName, "LastName");
             }
     }
}
+1  A: 

It's not possible to map a struct as a component.

You need to make it a class, or implement a IUserType.

Diego Mijelshon
Actually it is possible - I'm doing it now. But it does not seem possible to be possible if the struct is nullable.
cbp