Fluent NHibernate Map to private/protected Field that has no exposing Property
- by Jon Erickson
I have the following Person and Gender classes (I don't really, but the example is simplified to get my point across), using NHibernate (Fluent NHibernate) I want to map the Database Column "GenderId" [INT] value to the protected int _genderId field in my Person class. How do I do this?
FYI, the mappings and the domain objects are in separate assemblies.
public class Person : Entity
{
protected int _genderId;
public virtual int Id { get; private set; }
public virtual string Name { get; private set; }
public virtual Gender Gender
{
get { return Gender.FromId(_genderId); }
}
}
public class Gender : EnumerationBase<Gender>
{
public static Gender Male
= new Gender(1, "Male");
public static Gender Female
= new Gender(2, "Female");
private static readonly Gender[] _genders
= new[] { Male, Female };
private Gender(int id, string name)
{
Id = id;
Name = name;
}
public int Id { get; private set; }
public string Name { get; private set; }
public static Gender FromId(int id)
{
return _genders.Where(x => x.Id == id).SingleOrDefault();
}
}