Smooth NHIBERNATE: Mapping the Valid Value Type Attribute to Components

How to map nullable value type attributes to components 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; }
}
< br />public class PersonDbMap: ClassMap
{
public PersonDbMap()
{
/* This part doesn't compile! */
Component( x => x.Name,
part =>
{
part.Map(x => x.FirstName, "FirstName");
part.Map(x => x.LastName, "LastName");
}
}
}

It is impossible to map the structure as a component.

You need to set it as a class or implement IUserType.

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; }
}
< br />public class PersonDbMap: ClassMap
{
public PersonDbMap()
{
/* This part doesn't compile! */
Component( x => x.Name,
part =>
{
part.Map(x => x.FirstName, "FirstName");
part.Map(x => x.LastName, "LastName");
}
}
}

It is impossible to map structures to components

You need to set it as a class or implement IUserType.

Leave a Comment

Your email address will not be published.