流畅的NHibernate:将可空的值类型属性映射为组件

时间:2010-06-25 05:02:58

标签: nhibernate fluent-nhibernate

如何将可以为null的值类型属性映射为NHibernate中的组件?

例如:

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 个答案:

答案 0 :(得分:4)

无法将struct映射为组件。

您需要将其设为类,或实现IUserType